| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.Text.Json; |
| | 5 | | using System.Text.Json.Serialization; |
| | 6 | |
|
| | 7 | | namespace Jellyfin.Extensions.Json.Converters |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Convert delimited string to array of type. |
| | 11 | | /// </summary> |
| | 12 | | /// <typeparam name="T">Type to convert to.</typeparam> |
| | 13 | | public abstract class JsonDelimitedArrayConverter<T> : JsonConverter<T[]> |
| | 14 | | { |
| | 15 | | private readonly TypeConverter _typeConverter; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Initializes a new instance of the <see cref="JsonDelimitedArrayConverter{T}"/> class. |
| | 19 | | /// </summary> |
| 18 | 20 | | protected JsonDelimitedArrayConverter() |
| | 21 | | { |
| 18 | 22 | | _typeConverter = TypeDescriptor.GetConverter(typeof(T)); |
| 18 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Gets the array delimiter. |
| | 27 | | /// </summary> |
| | 28 | | protected virtual char Delimiter { get; } |
| | 29 | |
|
| | 30 | | /// <inheritdoc /> |
| | 31 | | public override T[]? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 32 | | { |
| 15 | 33 | | if (reader.TokenType == JsonTokenType.String) |
| | 34 | | { |
| | 35 | | // null got handled higher up the call stack |
| 11 | 36 | | var stringEntries = reader.GetString()!.Split(Delimiter, StringSplitOptions.RemoveEmptyEntries); |
| 11 | 37 | | if (stringEntries.Length == 0) |
| | 38 | | { |
| 1 | 39 | | return []; |
| | 40 | | } |
| | 41 | |
|
| 10 | 42 | | var typedValues = new List<T>(); |
| 70 | 43 | | for (var i = 0; i < stringEntries.Length; i++) |
| | 44 | | { |
| | 45 | | try |
| | 46 | | { |
| 25 | 47 | | var parsedValue = _typeConverter.ConvertFromInvariantString(stringEntries[i].Trim()); |
| 24 | 48 | | if (parsedValue is not null) |
| | 49 | | { |
| 24 | 50 | | typedValues.Add((T)parsedValue); |
| | 51 | | } |
| 24 | 52 | | } |
| 1 | 53 | | catch (FormatException) |
| | 54 | | { |
| | 55 | | // Ignore unconvertible inputs |
| 1 | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| 10 | 59 | | return typedValues.ToArray(); |
| | 60 | | } |
| | 61 | |
|
| 4 | 62 | | return JsonSerializer.Deserialize<T[]>(ref reader, options); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| | 66 | | public override void Write(Utf8JsonWriter writer, T[]? value, JsonSerializerOptions options) |
| | 67 | | { |
| 30 | 68 | | if (value is not null) |
| | 69 | | { |
| 30 | 70 | | writer.WriteStartArray(); |
| 30 | 71 | | if (value.Length > 0) |
| | 72 | | { |
| 0 | 73 | | foreach (var it in value) |
| | 74 | | { |
| 0 | 75 | | if (it is not null) |
| | 76 | | { |
| 0 | 77 | | writer.WriteStringValue(it.ToString()); |
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |
| | 81 | |
|
| 30 | 82 | | writer.WriteEndArray(); |
| | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 0 | 86 | | writer.WriteNullValue(); |
| | 87 | | } |
| 0 | 88 | | } |
| | 89 | | } |
| | 90 | | } |