mirror of
https://github.com/jellyfin/jellyfin
synced 2024-09-06 16:14:18 +00:00
Merge pull request #4391 from crobibero/converter-ireadonlylist
Support IReadOnlyList for JsonCommaDelimitedArrayConverter
This commit is contained in:
commit
f172b37401
|
@ -21,8 +21,8 @@ namespace MediaBrowser.Common.Json.Converters
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
var structType = typeToConvert.GetElementType();
|
var structType = typeToConvert.GetElementType() ?? typeToConvert.GenericTypeArguments[0];
|
||||||
return (JsonConverter)Activator.CreateInstance(typeof(JsonCommaDelimitedArrayConverter<>).MakeGenericType(structType));
|
return (JsonConverter)Activator.CreateInstance(typeof(JsonCommaDelimitedArrayConverter<>).MakeGenericType(structType));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,82 +11,82 @@ namespace Jellyfin.Common.Tests.Json
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void Deserialize_String_Valid_Success()
|
public static void Deserialize_String_Valid_Success()
|
||||||
{
|
{
|
||||||
var desiredValue = new GenericBodyModel<string>
|
var desiredValue = new GenericBodyArrayModel<string>
|
||||||
{
|
{
|
||||||
Value = new[] { "a", "b", "c" }
|
Value = new[] { "a", "b", "c" }
|
||||||
};
|
};
|
||||||
|
|
||||||
var options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions();
|
||||||
var value = JsonSerializer.Deserialize<GenericBodyModel<string>>(@"{ ""Value"": ""a,b,c"" }", options);
|
var value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a,b,c"" }", options);
|
||||||
Assert.Equal(desiredValue.Value, value?.Value);
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void Deserialize_String_Space_Valid_Success()
|
public static void Deserialize_String_Space_Valid_Success()
|
||||||
{
|
{
|
||||||
var desiredValue = new GenericBodyModel<string>
|
var desiredValue = new GenericBodyArrayModel<string>
|
||||||
{
|
{
|
||||||
Value = new[] { "a", "b", "c" }
|
Value = new[] { "a", "b", "c" }
|
||||||
};
|
};
|
||||||
|
|
||||||
var options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions();
|
||||||
var value = JsonSerializer.Deserialize<GenericBodyModel<string>>(@"{ ""Value"": ""a, b, c"" }", options);
|
var value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": ""a, b, c"" }", options);
|
||||||
Assert.Equal(desiredValue.Value, value?.Value);
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void Deserialize_GenericCommandType_Valid_Success()
|
public static void Deserialize_GenericCommandType_Valid_Success()
|
||||||
{
|
{
|
||||||
var desiredValue = new GenericBodyModel<GeneralCommandType>
|
var desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||||
{
|
{
|
||||||
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
||||||
};
|
};
|
||||||
|
|
||||||
var options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions();
|
||||||
options.Converters.Add(new JsonStringEnumConverter());
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
var value = JsonSerializer.Deserialize<GenericBodyModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", options);
|
var value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", options);
|
||||||
Assert.Equal(desiredValue.Value, value?.Value);
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void Deserialize_GenericCommandType_Space_Valid_Success()
|
public static void Deserialize_GenericCommandType_Space_Valid_Success()
|
||||||
{
|
{
|
||||||
var desiredValue = new GenericBodyModel<GeneralCommandType>
|
var desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||||
{
|
{
|
||||||
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
||||||
};
|
};
|
||||||
|
|
||||||
var options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions();
|
||||||
options.Converters.Add(new JsonStringEnumConverter());
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
var value = JsonSerializer.Deserialize<GenericBodyModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", options);
|
var value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", options);
|
||||||
Assert.Equal(desiredValue.Value, value?.Value);
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void Deserialize_String_Array_Valid_Success()
|
public static void Deserialize_String_Array_Valid_Success()
|
||||||
{
|
{
|
||||||
var desiredValue = new GenericBodyModel<string>
|
var desiredValue = new GenericBodyArrayModel<string>
|
||||||
{
|
{
|
||||||
Value = new[] { "a", "b", "c" }
|
Value = new[] { "a", "b", "c" }
|
||||||
};
|
};
|
||||||
|
|
||||||
var options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions();
|
||||||
var value = JsonSerializer.Deserialize<GenericBodyModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", options);
|
var value = JsonSerializer.Deserialize<GenericBodyArrayModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", options);
|
||||||
Assert.Equal(desiredValue.Value, value?.Value);
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public static void Deserialize_GenericCommandType_Array_Valid_Success()
|
public static void Deserialize_GenericCommandType_Array_Valid_Success()
|
||||||
{
|
{
|
||||||
var desiredValue = new GenericBodyModel<GeneralCommandType>
|
var desiredValue = new GenericBodyArrayModel<GeneralCommandType>
|
||||||
{
|
{
|
||||||
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
||||||
};
|
};
|
||||||
|
|
||||||
var options = new JsonSerializerOptions();
|
var options = new JsonSerializerOptions();
|
||||||
options.Converters.Add(new JsonStringEnumConverter());
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
var value = JsonSerializer.Deserialize<GenericBodyModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", options);
|
var value = JsonSerializer.Deserialize<GenericBodyArrayModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", options);
|
||||||
Assert.Equal(desiredValue.Value, value?.Value);
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using Jellyfin.Common.Tests.Models;
|
||||||
|
using MediaBrowser.Model.Session;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Jellyfin.Common.Tests.Json
|
||||||
|
{
|
||||||
|
public static class JsonCommaDelimitedIReadOnlyListTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public static void Deserialize_String_Valid_Success()
|
||||||
|
{
|
||||||
|
var desiredValue = new GenericBodyIReadOnlyListModel<string>
|
||||||
|
{
|
||||||
|
Value = new[] { "a", "b", "c" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions();
|
||||||
|
var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a,b,c"" }", options);
|
||||||
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public static void Deserialize_String_Space_Valid_Success()
|
||||||
|
{
|
||||||
|
var desiredValue = new GenericBodyIReadOnlyListModel<string>
|
||||||
|
{
|
||||||
|
Value = new[] { "a", "b", "c" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions();
|
||||||
|
var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": ""a, b, c"" }", options);
|
||||||
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public static void Deserialize_GenericCommandType_Valid_Success()
|
||||||
|
{
|
||||||
|
var desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>
|
||||||
|
{
|
||||||
|
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions();
|
||||||
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
|
var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp,MoveDown"" }", options);
|
||||||
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public static void Deserialize_GenericCommandType_Space_Valid_Success()
|
||||||
|
{
|
||||||
|
var desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>
|
||||||
|
{
|
||||||
|
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions();
|
||||||
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
|
var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": ""MoveUp, MoveDown"" }", options);
|
||||||
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public static void Deserialize_String_Array_Valid_Success()
|
||||||
|
{
|
||||||
|
var desiredValue = new GenericBodyIReadOnlyListModel<string>
|
||||||
|
{
|
||||||
|
Value = new[] { "a", "b", "c" }
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions();
|
||||||
|
var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<string>>(@"{ ""Value"": [""a"",""b"",""c""] }", options);
|
||||||
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public static void Deserialize_GenericCommandType_Array_Valid_Success()
|
||||||
|
{
|
||||||
|
var desiredValue = new GenericBodyIReadOnlyListModel<GeneralCommandType>
|
||||||
|
{
|
||||||
|
Value = new[] { GeneralCommandType.MoveUp, GeneralCommandType.MoveDown }
|
||||||
|
};
|
||||||
|
|
||||||
|
var options = new JsonSerializerOptions();
|
||||||
|
options.Converters.Add(new JsonStringEnumConverter());
|
||||||
|
var value = JsonSerializer.Deserialize<GenericBodyIReadOnlyListModel<GeneralCommandType>>(@"{ ""Value"": [""MoveUp"", ""MoveDown""] }", options);
|
||||||
|
Assert.Equal(desiredValue.Value, value?.Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ namespace Jellyfin.Common.Tests.Models
|
||||||
/// The generic body model.
|
/// The generic body model.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The value type.</typeparam>
|
/// <typeparam name="T">The value type.</typeparam>
|
||||||
public class GenericBodyModel<T>
|
public class GenericBodyArrayModel<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the value.
|
/// Gets or sets the value.
|
||||||
|
@ -17,4 +17,4 @@ namespace Jellyfin.Common.Tests.Models
|
||||||
[JsonConverter(typeof(JsonCommaDelimitedArrayConverterFactory))]
|
[JsonConverter(typeof(JsonCommaDelimitedArrayConverterFactory))]
|
||||||
public T[] Value { get; set; } = default!;
|
public T[] Value { get; set; } = default!;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using MediaBrowser.Common.Json.Converters;
|
||||||
|
|
||||||
|
namespace Jellyfin.Common.Tests.Models
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The generic body <c>IReadOnlyList</c> model.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The value type.</typeparam>
|
||||||
|
public class GenericBodyIReadOnlyListModel<T>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the value.
|
||||||
|
/// </summary>
|
||||||
|
[JsonConverter(typeof(JsonCommaDelimitedArrayConverterFactory))]
|
||||||
|
public IReadOnlyList<T> Value { get; set; } = default!;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue