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