-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.h
304 lines (263 loc) · 9.87 KB
/
json.h
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Copyright (C) Tammo Hinrichs 2021. All rights reserved.
// Licensed under the MIT License. See LICENSE.md file for full license information
//
#pragma once
#include "types.h"
// Usage: JSON_DEFINE_ENUM(MyEnum, ("My", "Enum", "Values"))
#define JSON_DEFINE_ENUM(type, strings) template<> struct JsonEnumDef<type> { \
static const Array<String> &GetValues() { static const Array<String> strs##strings; return strs; } \
JsonEnumDef(const type & r) : ref(r) { } \
const type & ref; \
};
#define JSON_BEGIN() template<class TV> void _VisitJson(TV &visitor) const { bool comma = false;
#define JSON_VALUE(v) visitor.Member(#v, v, comma); comma = true;
#define JSON_VALUE_NAME(v, name) visitor.Member(name, v, comma); comma = true;
#define JSON_ENUM(v)visitor.Member(#v, GetEnumDef(v), comma); comma = true;
#define JSON_ENUM_NAME(v, name) visitor.Member(name, GetEnumDef(v), comma); comma = true;
#define JSON_END() }
template<typename T> struct JsonEnumDef;
template<typename T> JsonEnumDef<T> GetEnumDef(const T& value) { return JsonEnumDef<T>(value); }
class Json
{
public:
template<typename T> static String Serialize(const T &value, bool pretty = false)
{
StringBuilder sb;
sb.SetPrettyPrint(pretty);
Write(sb, value);
return sb.ToString();
}
template<typename T> static bool Deserialize(const char *json, T &into)
{
Scanner scan(json);
Read(scan, into);
return !!scan;
}
template<typename T> static bool Deserialize(const char* json, T &into, Array<String> &errors)
{
Scanner scan(json);
Read(scan, into);
errors = scan.Errors();
return !!scan;
}
private:
//------------------------------------------------------------------
// reading
static void ReadJsonNumber(Scanner& scan, int64& major, int64& minor, int& minorDigits, int& exp, bool &sign)
{
major = 0; minor = 0; minorDigits = 0; exp = 0;
sign = scan.IfChar('-');
major = scan.Decimal();
if (scan.IfChar('.'))
minor = scan.Decimal(&minorDigits);
if (scan.IfChar('e') || scan.IfChar('E'))
{
bool sign2 = false;
scan.IfChar('+');
if (scan.IfChar('-'))
sign2 = true;
exp = (int)scan.Decimal();
if (sign2)
exp = -exp;
}
}
static void Read(Scanner& scan, double& value)
{
int64 major, minor;
int mdigits, exp;
bool sign;
ReadJsonNumber(scan, major, minor, mdigits, exp, sign);
value = ((double)major + (double)minor * pow(10, -mdigits)) * pow(10, exp) * (sign ? -1 : 1);
}
static void Read(Scanner& scan, int64& value)
{
int64 major, minor;
int mdigits, exp;
bool sign;
ReadJsonNumber(scan, major, minor, mdigits, exp, sign);
if (exp)
value = (int64)(((double)major + (double)minor * pow(10, -mdigits)) * pow(10, exp) * (sign ? -1 : 1));
else
value = major * (sign ? -1 : 1);
}
static void CheckRange(Scanner &scan, int64& v, int64 min, int64 max)
{
if (v < min)
{
v = min;
scan.Error(String::PrintF("Number too small (min: %lld)", min));
}
else if (v > max)
{
v = max;
scan.Error(String::PrintF("Number too large (mas: %lld)", max));
}
}
static void Read(Scanner& scan, float& value) { double v; Read(scan, v); value = (float)v; }
static void Read(Scanner& scan, int8& value) { int64 v; Read(scan, v); CheckRange(scan, v, -0x80, 0x7f); value = (int8)v; }
static void Read(Scanner& scan, uint8& value) { int64 v; Read(scan, v); CheckRange(scan, v, 0, 0xff); value = (uint8)v; }
static void Read(Scanner& scan, int16& value) { int64 v; Read(scan, v); CheckRange(scan, v, -0x8000, 0x7fff); value = (int16)v; }
static void Read(Scanner& scan, uint16& value) { int64 v; Read(scan, v); CheckRange(scan, v, 0, 0xffff); value = (uint16)v; }
static void Read(Scanner& scan, int& value) { int64 v; Read(scan, v); CheckRange(scan, v, -0x80000000ll, 0x7fffffffll); value = (int)v; }
static void Read(Scanner& scan, uint& value) { int64 v; Read(scan, v); CheckRange(scan, v, 0, 0xffffffffll); value = (uint)v; }
template<class T> static void Read(Scanner& scan, JsonEnumDef<T>& def)
{
String value = scan.QuotedString();
auto strs = def.GetValues();
for (int i = 0; i < strs.Count(); i++)
if (!value.Compare(strs[i], true))
{
const_cast<T&>(def.ref) = (T)i;
return;
}
scan.Error(String::PrintF("Unknown value %s (expected: %s)", (const char*)value, (const char*)String::Join(strs, ", ")));
}
template<class T> static void Read(Scanner& scan, Array<T>& array)
{
if (!scan.Char('[')) return;
array.Clear();
while (!scan.IfChar(']'))
{
T value;
Read(scan, value);
array += value;
scan.IfChar(',');
}
}
template<class T> struct ReadVisitor
{
ReadVisitor(Scanner &s, String n) : name(n), scan(s) {}
template<typename T> void Member(String mn, const T& value, bool)
{
if (!found && !name.Compare(mn, true))
{
Read(scan, const_cast<T&>(value));
found = true;
}
}
String name;
Scanner& scan;
bool found = false;
};
template<class T> static void Read(Scanner& scan, T& object)
{
if (!scan.Char('{')) return;
while (!scan.IfChar('}'))
{
String name = scan.QuotedString();
if (!scan.Char(':')) return;
ReadVisitor<T> visitor(scan, name);
object._VisitJson(visitor);
if (!visitor.found)
{
scan.Error(String::PrintF("Unknown key: %s", (const char*)name));
return;
}
scan.IfChar(',');
}
}
static void Read(Scanner& scan, bool& str)
{
if (scan.If("true")) str = true;
else if (scan.If("false")) str = false;
else scan.Error("Expected true or false");
}
static void Read(Scanner& scan, String& str) { str = scan.If("null") ? String() : scan.QuotedString(); }
//------------------------------------------------------------------
// writing
static void Write(StringBuilder& sb, const double& value) { sb += String::PrintF("%f", value); }
static void Write(StringBuilder& sb, const int64& value) { sb += String::PrintF("%lld", value); }
static void Write(StringBuilder& sb, const uint64& value) { sb += String::PrintF("%llu", value); }
static void Write(StringBuilder& sb, const bool& value) { sb += value ? "true" : "false"; }
static void Write(StringBuilder& sb, const int8& value) { Write(sb, (int64)value); }
static void Write(StringBuilder& sb, const uint8& value) { Write(sb, (uint64)value); }
static void Write(StringBuilder& sb, const int16& value) { Write(sb, (int64)value); }
static void Write(StringBuilder& sb, const uint16& value) { Write(sb, (uint64)value); }
static void Write(StringBuilder& sb, const int& value) { Write(sb, (int64)value); }
static void Write(StringBuilder& sb, const uint& value) { Write(sb, (uint64)value); }
static void Write(StringBuilder& sb, const float& value) { Write(sb, (double)value); }
static void Write(StringBuilder& sb, const char *value)
{
if (value)
{
char buffer[1024];
int bfill = 0;
auto addChar = [&](char c)
{
buffer[bfill++] = c;
if (bfill == 1023)
{
buffer[bfill] = 0;
sb += buffer;
bfill = 0;
}
};
sb += "\"";
while (*value)
{
switch (*value)
{
case '\\': case '\"': addChar('\\'); addChar(*value); break;
case '\r': addChar('\\'); addChar('r'); break;
case '\n': addChar('\\'); addChar('n'); break;
case '\t': addChar('\\'); addChar('t'); break;
default: if ((uint)*value >= ' ') addChar(*value); break;
}
value++;
}
buffer[bfill] = 0;
sb += buffer;
sb += "\"";
}
else
sb += "null";
}
static void Write(StringBuilder& sb, const String& value) { Write(sb, (const char*)value); }
template<class T> static void Write(StringBuilder& sb, const JsonEnumDef<T>& value)
{
Write(sb, JsonEnumDef<T>::GetValues()[(size_t)value.ref]);
}
template<class T> static void Write(StringBuilder& sb, const Array<T> &array)
{
sb += "[";
sb.PrettyNewline(2);
bool comma = false;
for (auto &vv : array)
{
if (comma)
{
sb += ",";
sb.PrettyNewline();
}
Write(sb, vv);
comma = true;
}
sb.PrettyNewline(-2);
sb += "]";
}
struct WriteVisitor
{
WriteVisitor(StringBuilder& into) : sb(into) {}
StringBuilder& sb;
template<typename T> void Member(String name, const T& value, bool comma)
{
if (comma) {
sb += ",";
sb.PrettyNewline();
}
sb.Append("\"", name, "\":");
sb.PrettySpace();
Write(sb, value);
}
};
template<typename T> static void Write(StringBuilder& sb, const T& object)
{
sb += "{";
sb.PrettyNewline(2);
WriteVisitor visitor(sb);
object._VisitJson(visitor);
sb.PrettyNewline(-2);
sb += "}";
}
};