-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJsonRangeConverter.cs
149 lines (121 loc) · 4.33 KB
/
JsonRangeConverter.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//-----------------------------------------------------------------------
// <copyright>
// Created by Matt Weber <matt@badecho.com>
// Copyright @ 2025 Bad Echo LLC. All rights reserved.
//
// Bad Echo Technologies are licensed under the
// GNU Affero General Public License v3.0.
//
// See accompanying file LICENSE.md or a copy at:
// https://www.gnu.org/licenses/agpl-3.0.html
// </copyright>
//-----------------------------------------------------------------------
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using BadEcho.Properties;
namespace BadEcho.Serialization;
/// <summary>
/// Provides a class for converting a range of numeric values to or from JSON.
/// </summary>
/// <typeparam name="T">The type of object or value handled by the converter.</typeparam>
/// <remarks>
/// <para>
/// This converter will produce a range of numeric values as described by the JSON input; refer to the example
/// for how these ranges should be formatted in JSON.
/// </para>
/// </remarks>
/// <example>Given the following JSON input:
/// <code>
/// "ranges": [
/// {
/// "start": 5,
/// "end": 20
/// },
/// {
/// "start": 80,
/// "end": 200
/// }
/// ]
/// </code>
/// <para>
/// The output from this converter will be a sequence containing all numbers from 5 to 20, as well as all numbers
/// from 80 to 200.
/// </para>
/// <para>
/// The names used for the <c>start</c> and <c>end</c> properties are inconsequential; all that matters is the order.
/// </para>
/// </example>
public sealed class JsonRangeConverter<T> : JsonConverter<IEnumerable<T>>
where T : unmanaged, IConvertible
{
/// <inheritdoc/>
public override IEnumerable<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException(Strings.JsonNotStartArray);
var ranges = new List<T>();
reader.Read();
while (reader.TokenType != JsonTokenType.EndArray)
{
ranges.AddRange(ReadRange(ref reader));
reader.Read();
}
return ranges;
}
/// <inheritdoc/>
public override void Write(Utf8JsonWriter writer, IEnumerable<T> value, JsonSerializerOptions options)
{
Require.NotNull(writer, nameof(writer));
Require.NotNull(value, nameof(value));
writer.WriteStartArray();
var range = new List<int>();
foreach (T valueInRange in value)
{
int numberInRange = valueInRange.ToInt32(CultureInfo.InvariantCulture);
// If this number comes immediately after the previous one, we add it to the current range.
if (range.Count == 0 || numberInRange - range[^1] <= 1)
range.Add(numberInRange);
else
{
WriteRange(writer, range[0], range[^1]);
range = [numberInRange];
}
}
if (range.Count > 0)
WriteRange(writer, range[0], range[^1]);
writer.WriteEndArray();
}
private static List<T> ReadRange(ref Utf8JsonReader reader)
{
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException(Strings.JsonNotStartObject);
var range = new List<T>();
int start = ReadRangeExtremum(ref reader);
int end = ReadRangeExtremum(ref reader);
reader.Read();
for (int i = start; i <= end; i++)
{
T value = (T) Convert.ChangeType(i, typeof(T), CultureInfo.InvariantCulture);
range.Add(value);
}
return range;
}
private static int ReadRangeExtremum(ref Utf8JsonReader reader)
{
reader.Read();
if (reader.TokenType != JsonTokenType.PropertyName)
throw new JsonException(Strings.JsonMalformedText);
reader.Read();
if (reader.TokenType != JsonTokenType.Number)
throw new JsonException(Strings.JsonExtremumValueNotNumber);
return reader.GetInt32();
}
private static void WriteRange(Utf8JsonWriter writer, int start, int end)
{
writer.WriteStartObject();
writer.WriteNumber(nameof(start), start);
writer.WriteNumber(nameof(end), end);
writer.WriteEndObject();
}
}