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
|
typedef struct JsonParser JsonParser;
struct JsonParser {
Arena *arena;
u8 *at;
u8 *end;
b32 failed;
u32 depth;
};
internal void json_skip_whitespace(JsonParser *parser)
{
while(parser->at < parser->end && (*parser->at == ' ' || *parser->at == '\n' || *parser->at == '\r' || *parser->at == '\t')) {
++parser->at;
}
}
internal u32 json_hex4(JsonParser *parser)
{
u32 value = 0;
for(u32 index = 0; index < 4; ++index) {
if(parser->at >= parser->end) {
parser->failed = true;
return 0;
}
u8 c = *parser->at++;
value <<= 4;
if(c >= '0' && c <= '9') value |= (u32)(c - '0');
else if(c >= 'a' && c <= 'f') value |= (u32)(c - 'a' + 10);
else if(c >= 'A' && c <= 'F') value |= (u32)(c - 'A' + 10);
else parser->failed = true;
}
return value;
}
internal u32 utf8_encode(u8 *out, u32 codepoint)
{
if(codepoint < 0x80) {
out[0] = (u8)codepoint;
return 1;
} else if(codepoint < 0x800) {
out[0] = (u8)(0xC0 | (codepoint >> 6));
out[1] = (u8)(0x80 | (codepoint & 0x3F));
return 2;
} else if(codepoint < 0x10000) {
out[0] = (u8)(0xE0 | (codepoint >> 12));
out[1] = (u8)(0x80 | ((codepoint >> 6) & 0x3F));
out[2] = (u8)(0x80 | (codepoint & 0x3F));
return 3;
}
out[0] = (u8)(0xF0 | (codepoint >> 18));
out[1] = (u8)(0x80 | ((codepoint >> 12) & 0x3F));
out[2] = (u8)(0x80 | ((codepoint >> 6) & 0x3F));
out[3] = (u8)(0x80 | (codepoint & 0x3F));
return 4;
}
internal String8 json_parse_string(JsonParser *parser)
{
// NOTE: caller already consumed the opening quote
u8 *start = parser->at;
b32 has_escapes = false;
while(parser->at < parser->end && *parser->at != '"') {
if(*parser->at == '\\') {
has_escapes = true;
++parser->at;
}
++parser->at;
}
if(parser->at >= parser->end) {
parser->failed = true;
return str8_zero();
}
u8 *stop = parser->at;
++parser->at; // closing quote
if(!has_escapes) {
return ToString8Size(start, stop - start);
}
// NOTE: unescaped output is never longer than the escaped input
u8 *out = PushArray(parser->arena, u8, (u64)(stop - start));
u64 length = 0;
for(u8 *at = start; at < stop;) {
if(*at != '\\') {
out[length++] = *at++;
continue;
}
++at;
u8 c = *at++;
switch(c) {
case 'n': out[length++] = '\n'; break;
case 't': out[length++] = '\t'; break;
case 'r': out[length++] = '\r'; break;
case 'b': out[length++] = '\b'; break;
case 'f': out[length++] = '\f'; break;
case 'u': {
JsonParser hex = *parser;
hex.at = at;
hex.end = stop;
u32 codepoint = json_hex4(&hex);
at = hex.at;
// surrogate pair
if(codepoint >= 0xD800 && codepoint <= 0xDBFF && at + 6 <= stop && at[0] == '\\' && at[1] == 'u') {
hex.at = at + 2;
u32 low = json_hex4(&hex);
at = hex.at;
codepoint = 0x10000 + ((codepoint - 0xD800) << 10) + (low - 0xDC00);
}
length += utf8_encode(out + length, codepoint);
} break;
default: out[length++] = c; break;
}
}
return ToString8Size(out, length);
}
internal JsonValue *json_parse_value(JsonParser *parser)
{
json_skip_whitespace(parser);
if(parser->failed || parser->at >= parser->end || parser->depth > 64) {
parser->failed = true;
return NULL;
}
JsonValue *value = PushStructZero(parser->arena, JsonValue);
u8 c = *parser->at;
if(c == '{' || c == '[') {
b32 is_object = (c == '{');
u8 close = is_object ? '}' : ']';
value->kind = is_object ? JSON_OBJECT : JSON_ARRAY;
++parser->at;
++parser->depth;
JsonValue *last = NULL;
json_skip_whitespace(parser);
if(parser->at < parser->end && *parser->at == close) {
++parser->at;
--parser->depth;
return value;
}
while(!parser->failed) {
String8 key = {};
if(is_object) {
json_skip_whitespace(parser);
if(parser->at >= parser->end || *parser->at != '"') {
parser->failed = true;
break;
}
++parser->at;
key = json_parse_string(parser);
json_skip_whitespace(parser);
if(parser->at >= parser->end || *parser->at != ':') {
parser->failed = true;
break;
}
++parser->at;
}
JsonValue *child = json_parse_value(parser);
if(!child) {
break;
}
child->key = key;
if(last) last->next = child;
else value->first = child;
last = child;
++value->count;
json_skip_whitespace(parser);
if(parser->at >= parser->end) {
parser->failed = true;
break;
}
if(*parser->at == ',') {
++parser->at;
continue;
}
if(*parser->at == close) {
++parser->at;
break;
}
parser->failed = true;
}
--parser->depth;
} else if(c == '"') {
++parser->at;
value->kind = JSON_STRING;
value->string = json_parse_string(parser);
} else if(c == 't' && parser->end - parser->at >= 4 && memcmp(parser->at, "true", 4) == 0) {
value->kind = JSON_BOOL;
value->boolean = true;
parser->at += 4;
} else if(c == 'f' && parser->end - parser->at >= 5 && memcmp(parser->at, "false", 5) == 0) {
value->kind = JSON_BOOL;
parser->at += 5;
} else if(c == 'n' && parser->end - parser->at >= 4 && memcmp(parser->at, "null", 4) == 0) {
value->kind = JSON_NULL;
parser->at += 4;
} else if(c == '-' || (c >= '0' && c <= '9')) {
// NOTE: strtod needs a terminator, numbers are short so copy into a stack buffer
char buffer[64];
u32 length = 0;
while(parser->at < parser->end && length < sizeof(buffer) - 1) {
u8 d = *parser->at;
if(!((d >= '0' && d <= '9') || d == '-' || d == '+' || d == '.' || d == 'e' || d == 'E')) {
break;
}
buffer[length++] = (char)d;
++parser->at;
}
buffer[length] = 0;
value->kind = JSON_NUMBER;
value->number = strtod(buffer, NULL);
} else {
parser->failed = true;
}
return parser->failed ? NULL : value;
}
internal JsonValue *json_parse(Arena *arena, String8 text)
{
JsonParser parser = {};
parser.arena = arena;
parser.at = text.data;
parser.end = text.data + text.size;
JsonValue *root = json_parse_value(&parser);
return parser.failed ? NULL : root;
}
internal JsonValue *json_get(JsonValue *object, const char *key)
{
if(!object || object->kind != JSON_OBJECT) {
return NULL;
}
String8 needle = str8(key);
FOR_JSON_CHILD(object, child) {
if(child->key.size == needle.size && memcmp(child->key.data, needle.data, needle.size) == 0) {
return child;
}
}
return NULL;
}
internal String8 json_get_string(JsonValue *object, const char *key)
{
JsonValue *value = json_get(object, key);
return (value && value->kind == JSON_STRING) ? value->string : str8_zero();
}
internal f64 json_get_number(JsonValue *object, const char *key)
{
JsonValue *value = json_get(object, key);
return (value && value->kind == JSON_NUMBER) ? value->number : 0.0;
}
|