2020-10-11 23:45:45 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Json.Converters
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert comma delimited string to array of type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Type to convert to.</typeparam>
|
|
|
|
|
public class JsonCommaDelimitedArrayConverter<T> : JsonConverter<T[]>
|
|
|
|
|
{
|
|
|
|
|
private readonly TypeConverter _typeConverter;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="JsonCommaDelimitedArrayConverter{T}"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public JsonCommaDelimitedArrayConverter()
|
|
|
|
|
{
|
|
|
|
|
_typeConverter = TypeDescriptor.GetConverter(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override T[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
|
|
|
{
|
2020-11-14 14:47:34 +00:00
|
|
|
|
var stringEntries = reader.GetString()?.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
2020-10-11 23:45:45 +00:00
|
|
|
|
if (stringEntries == null || stringEntries.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return Array.Empty<T>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 15:06:25 +00:00
|
|
|
|
var parsedValues = new object[stringEntries.Length];
|
|
|
|
|
var convertedCount = 0;
|
2020-10-11 23:45:45 +00:00
|
|
|
|
for (var i = 0; i < stringEntries.Length; i++)
|
|
|
|
|
{
|
2020-11-12 15:06:25 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
parsedValues[i] = _typeConverter.ConvertFrom(stringEntries[i].Trim());
|
|
|
|
|
convertedCount++;
|
|
|
|
|
}
|
|
|
|
|
catch (FormatException)
|
|
|
|
|
{
|
2020-11-17 16:57:08 +00:00
|
|
|
|
// TODO log when upgraded to .Net6
|
|
|
|
|
// https://github.com/dotnet/runtime/issues/42975
|
2020-11-12 15:06:25 +00:00
|
|
|
|
// _logger.LogWarning(e, "Error converting value.");
|
|
|
|
|
}
|
2020-10-11 23:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 15:06:25 +00:00
|
|
|
|
var typedValues = new T[convertedCount];
|
|
|
|
|
var typedValueIndex = 0;
|
|
|
|
|
for (var i = 0; i < stringEntries.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (parsedValues[i] != null)
|
|
|
|
|
{
|
|
|
|
|
typedValues.SetValue(parsedValues[i], typedValueIndex);
|
|
|
|
|
typedValueIndex++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return typedValues;
|
2020-10-11 23:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return JsonSerializer.Deserialize<T[]>(ref reader, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, T[] value, JsonSerializerOptions options)
|
|
|
|
|
{
|
|
|
|
|
JsonSerializer.Serialize(writer, value, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-14 14:47:34 +00:00
|
|
|
|
}
|