-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathJsonDelegatedStringConverter.cs
59 lines (53 loc) · 2.25 KB
/
JsonDelegatedStringConverter.cs
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
using Macross.Json.Extensions;
namespace System.Text.Json.Serialization
{
/// <summary>
/// A <see cref="JsonConverter{T}"/> that uses conversion functions to transpose types to and from JSON strings.
/// </summary>
/// <typeparam name="T">The type being converted.</typeparam>
public class JsonDelegatedStringConverter<T> : JsonConverter<T>
where T : notnull
{
private readonly Func<string, T> _ConvertValueFromStringFunc;
private readonly Func<T, string> _ConvertValueToStringFunc;
/// <summary>
/// Initializes a new instance of the <see cref="JsonDelegatedStringConverter{T}"/> class.
/// </summary>
/// <param name="convertValueFromStringFunc">A function for converting <typeparamref name="T"/> values into strings.</param>
/// <param name="convertValueToStringFunc">A function for converting strings into <typeparamref name="T"/> values.</param>
public JsonDelegatedStringConverter(Func<string, T> convertValueFromStringFunc, Func<T, string> convertValueToStringFunc)
{
_ConvertValueFromStringFunc = convertValueFromStringFunc ?? throw new ArgumentNullException(nameof(convertValueFromStringFunc));
_ConvertValueToStringFunc = convertValueToStringFunc ?? throw new ArgumentNullException(nameof(convertValueToStringFunc));
}
/// <inheritdoc/>
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(T));
string value = reader.GetString()!;
try
{
return _ConvertValueFromStringFunc(value);
}
catch (Exception ex)
{
throw ThrowHelper.GenerateJsonException_DeserializeUnableToConvertValue(typeof(T), value, ex);
}
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
#pragma warning disable CA1062 // Don't perform checks for performance. Trust our callers will be nice.
{
try
{
writer.WriteStringValue(_ConvertValueToStringFunc(value));
}
catch (Exception ex)
{
throw new JsonException($"Value '{value}' of {typeof(T)} type could not be converted into a JSON string.", ex);
}
}
#pragma warning restore CA1062 // Validate arguments of public methods
}
}