mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-14 06:06:41 +00:00
Add MediaStreamProtocol enum (#10153)
* Add MediaStreamProtocol enum * Add default handling for enum during deserialization --------- Co-authored-by: Cody Robibero <cody@robibe.ro>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Json unknown enum converter.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of enum.</typeparam>
|
||||
public class JsonDefaultStringEnumConverter<T> : JsonConverter<T>
|
||||
where T : struct, Enum
|
||||
{
|
||||
private readonly JsonConverter<T> _baseConverter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonDefaultStringEnumConverter{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="baseConverter">The base json converter.</param>
|
||||
public JsonDefaultStringEnumConverter(JsonConverter<T> baseConverter)
|
||||
{
|
||||
_baseConverter = baseConverter;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNull() || reader.IsEmptyString())
|
||||
{
|
||||
var customValueAttribute = typeToConvert.GetCustomAttribute<DefaultValueAttribute>();
|
||||
if (customValueAttribute?.Value is null)
|
||||
{
|
||||
throw new InvalidOperationException($"Default value not set for '{typeToConvert.Name}'");
|
||||
}
|
||||
|
||||
return (T)customValueAttribute.Value;
|
||||
}
|
||||
|
||||
return _baseConverter.Read(ref reader, typeToConvert, options);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
_baseConverter.Write(writer, value, options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Jellyfin.Extensions.Json.Converters;
|
||||
|
||||
/// <summary>
|
||||
/// Utilizes the JsonStringEnumConverter and sets a default value if not provided.
|
||||
/// </summary>
|
||||
public class JsonDefaultStringEnumConverterFactory : JsonConverterFactory
|
||||
{
|
||||
private static readonly JsonStringEnumConverter _baseConverterFactory = new();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
return _baseConverterFactory.CanConvert(typeToConvert)
|
||||
&& typeToConvert.IsDefined(typeof(DefaultValueAttribute));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var baseConverter = _baseConverterFactory.CreateConverter(typeToConvert, options);
|
||||
var converterType = typeof(JsonDefaultStringEnumConverter<>).MakeGenericType(typeToConvert);
|
||||
|
||||
return (JsonConverter?)Activator.CreateInstance(converterType, baseConverter);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override Guid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
=> reader.TokenType == JsonTokenType.Null
|
||||
=> reader.IsNull()
|
||||
? Guid.Empty
|
||||
: ReadInternal(ref reader);
|
||||
|
||||
|
||||
@@ -15,10 +15,7 @@ namespace Jellyfin.Extensions.Json.Converters
|
||||
/// <inheritdoc />
|
||||
public override TStruct? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
// Token is empty string.
|
||||
if (reader.TokenType == JsonTokenType.String
|
||||
&& ((reader.HasValueSequence && reader.ValueSequence.IsEmpty)
|
||||
|| (!reader.HasValueSequence && reader.ValueSpan.IsEmpty)))
|
||||
if (reader.IsEmptyString())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ namespace Jellyfin.Extensions.Json
|
||||
new JsonNullableGuidConverter(),
|
||||
new JsonVersionConverter(),
|
||||
new JsonFlagEnumConverterFactory(),
|
||||
new JsonDefaultStringEnumConverterFactory(),
|
||||
new JsonStringEnumConverter(),
|
||||
new JsonNullableStructConverterFactory(),
|
||||
new JsonDateTimeConverter(),
|
||||
|
||||
27
src/Jellyfin.Extensions/Json/Utf8JsonExtensions.cs
Normal file
27
src/Jellyfin.Extensions/Json/Utf8JsonExtensions.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Jellyfin.Extensions.Json;
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for Utf8JsonReader and Utf8JsonWriter.
|
||||
/// </summary>
|
||||
public static class Utf8JsonExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines if the reader contains an empty string.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns>Whether the reader contains an empty string.</returns>
|
||||
public static bool IsEmptyString(this Utf8JsonReader reader)
|
||||
=> reader.TokenType == JsonTokenType.String
|
||||
&& ((reader.HasValueSequence && reader.ValueSequence.IsEmpty)
|
||||
|| (!reader.HasValueSequence && reader.ValueSpan.IsEmpty));
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the reader contains a null value.
|
||||
/// </summary>
|
||||
/// <param name="reader">The reader.</param>
|
||||
/// <returns>Whether the reader contains null.</returns>
|
||||
public static bool IsNull(this Utf8JsonReader reader)
|
||||
=> reader.TokenType == JsonTokenType.Null;
|
||||
}
|
||||
Reference in New Issue
Block a user