Skip to content

Commit

Permalink
Merge pull request #2436 from bobrippling/fix/rjson-obj-parsing
Browse files Browse the repository at this point in the history
Fix RJSON's object parsing
  • Loading branch information
gfwilliams authored Nov 27, 2023
2 parents 5440541 + 4dd95bf commit 96f97e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/jswrap_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ JsVar *jswrap_json_parse_internal(JSONFlags flags) {
case '{': {
JsVar *obj = jsvNewObject(); if (!obj) return 0;
jslGetNextToken(); // {
while (lex->tk == LEX_STR || lex->tk == LEX_ID && !jspHasError()) {
if (!(flags&JSON_DROP_QUOTES)) {
jslMatch(LEX_STR);
return obj;
while ((lex->tk == LEX_STR || lex->tk == LEX_ID) && !jspHasError()) {
if (!(flags&JSON_DROP_QUOTES) && !jslMatch(LEX_STR)) {
jsvUnLock(obj);
return 0;
}
JsVar *key = jsvAsArrayIndexAndUnLock(jslGetTokenValueAsVar());
jslGetNextToken();
Expand Down
23 changes: 22 additions & 1 deletion tests/test_json_object.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
// JSON shouldn't print stuff like __proto__ and constructor

function A() {}
function A() {}
var a = new A();

result = JSON.stringify(a)=="{}";

function assertEq(got, expected) {
if (typeof got !== typeof expected)
throw new Error("mismatch, " + typeof got + " != " + typeof expected);

if (typeof got === "object")
// note: doesn't check keys in `got`
for (var k in expected)
assertEq(got[k], expected[k]);
else if (got !== expected)
throw new Error("mismatch, " + got + " != " + expected);
}

// no exceptions thrown:
assertEq(JSON.parse('{"a": 1}'), {"a": 1});
assertEq(JSON.parse('["4", 5, "six"]'), ["4", 5, "six"]);
assertEq(JSON.parse('["4", 5, "six", {"x": 5}]'), ["4", 5, "six", {"x": 5}]);
assertEq(JSON.parse('""'), "");
assertEq(JSON.parse('5'), 5);
assertEq(JSON.parse('[]'), []);
assertEq(JSON.parse('{}'), {});

0 comments on commit 96f97e6

Please sign in to comment.