2024-08-30 15:29:48 +02:00
|
|
|
using System;
|
2024-09-05 12:55:15 +02:00
|
|
|
using System.Collections.Generic;
|
2021-05-06 23:16:52 +02:00
|
|
|
using System.ComponentModel;
|
2024-09-05 12:55:15 +02:00
|
|
|
using System.Linq;
|
2021-05-06 23:16:52 +02:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
2021-06-19 18:02:33 +02:00
|
|
|
namespace Jellyfin.Extensions.Json.Converters
|
2021-05-06 23:16:52 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert delimited string to array of type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Type to convert to.</typeparam>
|
2021-12-19 02:17:32 +01:00
|
|
|
public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]>
|
2021-05-06 23:16:52 +02:00
|
|
|
{
|
|
|
|
|
private readonly TypeConverter _typeConverter;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="JsonDelimitedArrayConverter{T}"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected JsonDelimitedArrayConverter()
|
|
|
|
|
{
|
|
|
|
|
_typeConverter = TypeDescriptor.GetConverter(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the array delimiter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual char Delimiter { get; }
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
|
|
|
{
|
2021-12-19 02:17:32 +01:00
|
|
|
// null got handled higher up the call stack
|
|
|
|
|
var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (stringEntries.Length == 0)
|
2021-05-06 23:16:52 +02:00
|
|
|
{
|
2024-09-05 12:55:15 +02:00
|
|
|
return [];
|
2021-05-06 23:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 12:55:15 +02:00
|
|
|
var typedValues = new List<T>();
|
2021-05-06 23:16:52 +02:00
|
|
|
for (var i = 0; i < stringEntries.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-09-05 12:55:15 +02:00
|
|
|
var parsedValue = _typeConverter.ConvertFromInvariantString(stringEntries[i].Trim());
|
|
|
|
|
if (parsedValue is not null)
|
|
|
|
|
{
|
|
|
|
|
typedValues.Add((T)parsedValue);
|
|
|
|
|
}
|
2021-05-06 23:16:52 +02:00
|
|
|
}
|
|
|
|
|
catch (FormatException)
|
|
|
|
|
{
|
2024-09-05 12:55:15 +02:00
|
|
|
// Ignore unconvertable inputs
|
2021-05-06 23:16:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 16:46:59 +02:00
|
|
|
return typedValues.ToArray();
|
2021-05-06 23:16:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return JsonSerializer.Deserialize<T[]>(ref reader, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, T[]? value, JsonSerializerOptions options)
|
|
|
|
|
{
|
2024-09-05 12:55:15 +02:00
|
|
|
if (value is not null)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteStartArray();
|
|
|
|
|
if (value.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var it in value)
|
|
|
|
|
{
|
|
|
|
|
if (it is not null)
|
|
|
|
|
{
|
|
|
|
|
writer.WriteStringValue(it.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.WriteEndArray();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
writer.WriteNullValue();
|
|
|
|
|
}
|
2021-05-06 23:16:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|