Add MediaStreamProtocol enum (#10153)
* Add MediaStreamProtocol enum * Add default handling for enum during deserialization --------- Co-authored-by: Cody Robibero <cody@robibe.ro>pull/11109/head
parent
83d2bc3f9f
commit
407cf5d0bf
@ -0,0 +1,20 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Jellyfin.Data.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// Media streaming protocol.
|
||||
/// </summary>
|
||||
[DefaultValue(Http)]
|
||||
public enum MediaStreamProtocol
|
||||
{
|
||||
/// <summary>
|
||||
/// HTTP.
|
||||
/// </summary>
|
||||
Http = 0,
|
||||
|
||||
/// <summary>
|
||||
/// HTTP Live Streaming.
|
||||
/// </summary>
|
||||
Hls = 1
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
using System.Text.Json;
|
||||
using Jellyfin.Data.Enums;
|
||||
using Jellyfin.Extensions.Json.Converters;
|
||||
using Xunit;
|
||||
|
||||
namespace Jellyfin.Extensions.Tests.Json.Converters;
|
||||
|
||||
public class JsonDefaultStringEnumConverterTests
|
||||
{
|
||||
private readonly JsonSerializerOptions _jsonOptions = new() { Converters = { new JsonDefaultStringEnumConverterFactory() } };
|
||||
|
||||
/// <summary>
|
||||
/// Test to ensure that `null` and empty string are deserialized to the default value.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string.</param>
|
||||
/// <param name="output">The expected enum value.</param>
|
||||
[Theory]
|
||||
[InlineData("\"\"", MediaStreamProtocol.Http)]
|
||||
[InlineData("\"Http\"", MediaStreamProtocol.Http)]
|
||||
[InlineData("\"Hls\"", MediaStreamProtocol.Hls)]
|
||||
public void Deserialize_Enum_Direct(string input, MediaStreamProtocol output)
|
||||
{
|
||||
var value = JsonSerializer.Deserialize<MediaStreamProtocol>(input, _jsonOptions);
|
||||
Assert.Equal(output, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test to ensure that `null` and empty string are deserialized to the default value.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string.</param>
|
||||
/// <param name="output">The expected enum value.</param>
|
||||
[Theory]
|
||||
[InlineData(null, MediaStreamProtocol.Http)]
|
||||
[InlineData("\"\"", MediaStreamProtocol.Http)]
|
||||
[InlineData("\"Http\"", MediaStreamProtocol.Http)]
|
||||
[InlineData("\"Hls\"", MediaStreamProtocol.Hls)]
|
||||
public void Deserialize_Enum(string? input, MediaStreamProtocol output)
|
||||
{
|
||||
input ??= "null";
|
||||
var json = $"{{ \"EnumValue\": {input} }}";
|
||||
var value = JsonSerializer.Deserialize<TestClass>(json, _jsonOptions);
|
||||
Assert.NotNull(value);
|
||||
Assert.Equal(output, value.EnumValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test to ensure that empty string is deserialized to the default value,
|
||||
/// and `null` is deserialized to `null`.
|
||||
/// </summary>
|
||||
/// <param name="input">The input string.</param>
|
||||
/// <param name="output">The expected enum value.</param>
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData("\"\"", MediaStreamProtocol.Http)]
|
||||
[InlineData("\"Http\"", MediaStreamProtocol.Http)]
|
||||
[InlineData("\"Hls\"", MediaStreamProtocol.Hls)]
|
||||
public void Deserialize_Enum_Nullable(string? input, MediaStreamProtocol? output)
|
||||
{
|
||||
input ??= "null";
|
||||
var json = $"{{ \"EnumValue\": {input} }}";
|
||||
var value = JsonSerializer.Deserialize<NullTestClass>(json, _jsonOptions);
|
||||
Assert.NotNull(value);
|
||||
Assert.Equal(output, value.EnumValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the roundtrip serialization & deserialization is successful.
|
||||
/// </summary>
|
||||
/// <param name="input">Input enum.</param>
|
||||
/// <param name="output">Output enum.</param>
|
||||
[Theory]
|
||||
[InlineData(MediaStreamProtocol.Http, MediaStreamProtocol.Http)]
|
||||
[InlineData(MediaStreamProtocol.Hls, MediaStreamProtocol.Hls)]
|
||||
public void Enum_RoundTrip(MediaStreamProtocol input, MediaStreamProtocol output)
|
||||
{
|
||||
var inputObj = new TestClass { EnumValue = input };
|
||||
|
||||
var outputObj = JsonSerializer.Deserialize<TestClass>(JsonSerializer.Serialize(inputObj, _jsonOptions), _jsonOptions);
|
||||
|
||||
Assert.NotNull(outputObj);
|
||||
Assert.Equal(output, outputObj.EnumValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that the roundtrip serialization & deserialization is successful, including null.
|
||||
/// </summary>
|
||||
/// <param name="input">Input enum.</param>
|
||||
/// <param name="output">Output enum.</param>
|
||||
[Theory]
|
||||
[InlineData(MediaStreamProtocol.Http, MediaStreamProtocol.Http)]
|
||||
[InlineData(MediaStreamProtocol.Hls, MediaStreamProtocol.Hls)]
|
||||
[InlineData(null, null)]
|
||||
public void Enum_RoundTrip_Nullable(MediaStreamProtocol? input, MediaStreamProtocol? output)
|
||||
{
|
||||
var inputObj = new NullTestClass { EnumValue = input };
|
||||
|
||||
var outputObj = JsonSerializer.Deserialize<NullTestClass>(JsonSerializer.Serialize(inputObj, _jsonOptions), _jsonOptions);
|
||||
|
||||
Assert.NotNull(outputObj);
|
||||
Assert.Equal(output, outputObj.EnumValue);
|
||||
}
|
||||
|
||||
private sealed class TestClass
|
||||
{
|
||||
public MediaStreamProtocol EnumValue { get; set; }
|
||||
}
|
||||
|
||||
private sealed class NullTestClass
|
||||
{
|
||||
public MediaStreamProtocol? EnumValue { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in new issue