mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-05-01 06:16:30 +01:00
Merge branch 'master' into update-plugin
This commit is contained in:
@@ -8,37 +8,38 @@ namespace MediaBrowser.Common.Json.Converters
|
||||
/// Converts a nullable struct or value to/from JSON.
|
||||
/// Required - some clients send an empty string.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The struct type.</typeparam>
|
||||
public class JsonNullableStructConverter<T> : JsonConverter<T?>
|
||||
where T : struct
|
||||
/// <typeparam name="TStruct">The struct type.</typeparam>
|
||||
public class JsonNullableStructConverter<TStruct> : JsonConverter<TStruct?>
|
||||
where TStruct : struct
|
||||
{
|
||||
private readonly JsonConverter<T?> _baseJsonConverter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonNullableStructConverter{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="baseJsonConverter">The base json converter.</param>
|
||||
public JsonNullableStructConverter(JsonConverter<T?> baseJsonConverter)
|
||||
{
|
||||
_baseJsonConverter = baseJsonConverter;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
// Handle empty string.
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Token is empty string.
|
||||
if (reader.TokenType == JsonTokenType.String && ((reader.HasValueSequence && reader.ValueSequence.IsEmpty) || reader.ValueSpan.IsEmpty))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return _baseJsonConverter.Read(ref reader, typeToConvert, options);
|
||||
return JsonSerializer.Deserialize<TStruct>(ref reader, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOptions options)
|
||||
public override void Write(Utf8JsonWriter writer, TStruct? value, JsonSerializerOptions options)
|
||||
{
|
||||
_baseJsonConverter.Write(writer, value, options);
|
||||
if (value.HasValue)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, value.Value, options);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Json.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Json nullable struct converter factory.
|
||||
/// </summary>
|
||||
public class JsonNullableStructConverterFactory : JsonConverterFactory
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return typeToConvert.IsGenericType
|
||||
&& typeToConvert.GetGenericTypeDefinition() == typeof(Nullable<>)
|
||||
&& typeToConvert.GenericTypeArguments[0].IsValueType;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var structType = typeToConvert.GenericTypeArguments[0];
|
||||
return (JsonConverter)Activator.CreateInstance(typeof(JsonNullableStructConverter<>).MakeGenericType(structType));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,14 +39,9 @@ namespace MediaBrowser.Common.Json
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||
};
|
||||
|
||||
// Get built-in converters for fallback converting.
|
||||
var baseNullableInt32Converter = (JsonConverter<int?>)options.GetConverter(typeof(int?));
|
||||
var baseNullableInt64Converter = (JsonConverter<long?>)options.GetConverter(typeof(long?));
|
||||
|
||||
options.Converters.Add(new JsonGuidConverter());
|
||||
options.Converters.Add(new JsonStringEnumConverter());
|
||||
options.Converters.Add(new JsonNullableStructConverter<int>(baseNullableInt32Converter));
|
||||
options.Converters.Add(new JsonNullableStructConverter<long>(baseNullableInt64Converter));
|
||||
options.Converters.Add(new JsonNullableStructConverterFactory());
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.9" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.9" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
|
||||
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.8" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
@@ -140,6 +141,30 @@ namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
ApplicationPaths = applicationPaths;
|
||||
XmlSerializer = xmlSerializer;
|
||||
if (this is IPluginAssembly assemblyPlugin)
|
||||
{
|
||||
var assembly = GetType().Assembly;
|
||||
var assemblyName = assembly.GetName();
|
||||
var assemblyFilePath = assembly.Location;
|
||||
|
||||
var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
|
||||
|
||||
assemblyPlugin.SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
|
||||
|
||||
var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
|
||||
if (idAttributes.Length > 0)
|
||||
{
|
||||
var attribute = (GuidAttribute)idAttributes[0];
|
||||
var assemblyId = new Guid(attribute.Value);
|
||||
|
||||
assemblyPlugin.SetId(assemblyId);
|
||||
}
|
||||
}
|
||||
|
||||
if (this is IHasPluginConfiguration hasPluginConfiguration)
|
||||
{
|
||||
hasPluginConfiguration.SetStartupInfo(s => Directory.CreateDirectory(s));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user