-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonConverters.cs
172 lines (147 loc) · 4.06 KB
/
JsonConverters.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ZwiftPower
{
public class UnixDateTimeConverter : JsonConverter<DateTime>
{
internal static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
long seconds;
if (reader.TokenType == JsonTokenType.Number)
{
seconds = reader.GetInt64();
}
else if (reader.TokenType == JsonTokenType.String)
{
if (long.TryParse(reader.GetString(), out seconds) == false)
{
throw new JsonException("Cannot convert to UNIX DateTime");
}
}
else
{
throw new JsonException("Unexpected token parsing date");
}
if (seconds > 0)
{
return UnixEpoch.AddSeconds(seconds);
}
else
{
throw new JsonException("Cannot convert value that is before the UNIX epoch");
}
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public class NumberBooleanConverter : JsonConverter<bool>
{
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32() == 1;
}
if (reader.TokenType == JsonTokenType.String)
{
if (int.TryParse(reader.GetString(), out int value) == false)
{
throw new JsonException("Invalid boolean value");
}
return value == 1;
}
throw new JsonException("Expected string or number");
}
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public class NullableNumberAsStringConverter : JsonConverter<int?>
{
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32();
}
if (reader.TokenType == JsonTokenType.String)
{
var value = reader.GetString();
return value == string.Empty ? null : int.Parse(value);
}
throw new JsonException("Expected empty string or number");
}
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public class NullableFloatAsStringConverter : JsonConverter<float?>
{
public override float? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetSingle();
}
if (reader.TokenType == JsonTokenType.String)
{
var value = reader.GetString();
return value == string.Empty ? null : float.Parse(value);
}
throw new JsonException("Expected empty string or number");
}
public override void Write(Utf8JsonWriter writer, float? value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
public class IntAsStringArrayConverter : JsonConverter<int[]>
{
public override int[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
List<int> values = new();
if (reader.TokenType == JsonTokenType.StartArray)
{
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.Number)
{
values.Add(reader.GetInt32());
}
else if (reader.TokenType == JsonTokenType.String)
{
var value = reader.GetString();
if (value == string.Empty)
{
values.Add(default);
}
else
{
values.Add(int.Parse(value));
}
}
else if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}
else
{
throw new JsonException("Expected number or string or end of array");
}
}
}
return values.ToArray();
}
public override void Write(Utf8JsonWriter writer, int[] value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}