Skip to content

Commit f756843

Browse files
committed
json.h update :
- enable quoted and unquoted keys - skip escaped chars (cause of early parse abort)
1 parent 6ddacde commit f756843

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/support/json.h

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,14 @@ struct Value {
261261
if (*curr == '"') {
262262
// String
263263
curr++;
264-
char* close = strchr(curr, '"');
265-
assert(close);
264+
char* close = curr;
265+
while (*close && *close != '"') {
266+
if (*close == '\\' && *(close + 1)) {
267+
close++; // Skip escaped character
268+
}
269+
close++;
270+
}
271+
assert(*close == '"');
266272
*close = 0; // end this string, and reuse it straight from the input
267273
setString(curr);
268274
curr = close + 1;
@@ -305,20 +311,40 @@ struct Value {
305311
skip();
306312
setObject();
307313
while (*curr != '}') {
308-
assert(*curr == '"');
309-
curr++;
310-
char* close = strchr(curr, '"');
311-
assert(close);
312-
*close = 0; // end this string, and reuse it straight from the input
313-
IString key(curr);
314-
curr = close + 1;
315-
skip();
316-
assert(*curr == ':');
317-
curr++;
318-
skip();
319-
Ref value = Ref(new Value());
320-
curr = value->parse(curr);
321-
(*obj)[key] = value;
314+
if (*curr == '"') {
315+
curr++;
316+
char* close = curr;
317+
while (*close && *close != '"') {
318+
if (*close == '\\' && *(close + 1)) {
319+
close++; // Skip escaped character
320+
}
321+
close++;
322+
}
323+
assert(*close == '"');
324+
*close = 0; // end this string, and reuse it straight from the input
325+
IString key(curr);
326+
curr = close + 1;
327+
skip();
328+
assert(*curr == ':');
329+
curr++;
330+
skip();
331+
Ref value = Ref(new Value());
332+
curr = value->parse(curr);
333+
(*obj)[key] = value;
334+
} else {
335+
// Unquoted key
336+
char* start = curr;
337+
while (*curr && *curr != ':' && !is_json_space(*curr)) {
338+
curr++;
339+
}
340+
assert(*curr == ':');
341+
IString key(std::string(start, curr - start).c_str());
342+
curr++;
343+
skip();
344+
Ref value = Ref(new Value());
345+
curr = value->parse(curr);
346+
(*obj)[key] = value;
347+
}
322348
skip();
323349
if (*curr == '}') {
324350
break;

0 commit comments

Comments
 (0)