| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | | using System.Text.Json; |
| | 4 | | using System.Text.Json.Serialization; |
| | 5 | |
|
| | 6 | | namespace Jellyfin.Extensions.Json.Converters |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Convert delimited string to array of type. |
| | 10 | | /// </summary> |
| | 11 | | /// <typeparam name="T">Type to convert to.</typeparam> |
| | 12 | | public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]> |
| | 13 | | { |
| | 14 | | private readonly TypeConverter _typeConverter; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Initializes a new instance of the <see cref="JsonDelimitedArrayConverter{T}"/> class. |
| | 18 | | /// </summary> |
| 16 | 19 | | protected JsonDelimitedArrayConverter() |
| | 20 | | { |
| 16 | 21 | | _typeConverter = TypeDescriptor.GetConverter(typeof(T)); |
| 16 | 22 | | } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Gets the array delimiter. |
| | 26 | | /// </summary> |
| | 27 | | protected virtual char Delimiter { get; } |
| | 28 | |
|
| | 29 | | /// <inheritdoc /> |
| | 30 | | public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 31 | | { |
| 15 | 32 | | if (reader.TokenType == JsonTokenType.String) |
| | 33 | | { |
| | 34 | | // null got handled higher up the call stack |
| 11 | 35 | | var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries); |
| 11 | 36 | | if (stringEntries.Length == 0) |
| | 37 | | { |
| 1 | 38 | | return Array.Empty<T>(); |
| | 39 | | } |
| | 40 | |
|
| 10 | 41 | | var parsedValues = new object[stringEntries.Length]; |
| 10 | 42 | | var convertedCount = 0; |
| 70 | 43 | | for (var i = 0; i < stringEntries.Length; i++) |
| | 44 | | { |
| | 45 | | try |
| | 46 | | { |
| 25 | 47 | | parsedValues[i] = _typeConverter.ConvertFromInvariantString(stringEntries[i].Trim()) ?? throw ne |
| 24 | 48 | | convertedCount++; |
| 24 | 49 | | } |
| 1 | 50 | | catch (FormatException) |
| | 51 | | { |
| | 52 | | // TODO log when upgraded to .Net6 |
| | 53 | | // https://github.com/dotnet/runtime/issues/42975 |
| | 54 | | // _logger.LogDebug(e, "Error converting value."); |
| 1 | 55 | | } |
| | 56 | | } |
| | 57 | |
|
| 10 | 58 | | var typedValues = new T[convertedCount]; |
| 10 | 59 | | var typedValueIndex = 0; |
| 70 | 60 | | for (var i = 0; i < stringEntries.Length; i++) |
| | 61 | | { |
| 25 | 62 | | if (parsedValues[i] is not null) |
| | 63 | | { |
| 24 | 64 | | typedValues.SetValue(parsedValues[i], typedValueIndex); |
| 24 | 65 | | typedValueIndex++; |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|
| 10 | 69 | | return typedValues; |
| | 70 | | } |
| | 71 | |
|
| 4 | 72 | | return JsonSerializer.Deserialize<T[]>(ref reader, options); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <inheritdoc /> |
| | 76 | | public override void Write(Utf8JsonWriter writer, T[]? value, JsonSerializerOptions options) |
| | 77 | | { |
| 0 | 78 | | throw new NotImplementedException(); |
| | 79 | | } |
| | 80 | | } |
| | 81 | | } |