Skip to content

Commit

Permalink
Allow empty strings in strict JSON parsing, fixes #16255 !strict
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Feb 28, 2013
1 parent 7faf65d commit 54f9a48
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion json.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ define(["./has"], function(has){
};
return {
parse: has("json-parse") ? JSON.parse : function(str, strict){
if(strict && !/^([\s\[\{]*(?:"(?:\\.|[^"])+"|-?\d[\d\.]*(?:[Ee][+-]?\d+)?|null|true|false|)[\s\]\}]*(?:,|:|$))+$/.test(str)){
if(strict && !/^([\s\[\{]*(?:"(?:\\.|[^"])*"|-?\d[\d\.]*(?:[Ee][+-]?\d+)?|null|true|false|)[\s\]\}]*(?:,|:|$))+$/.test(str)){
throw new SyntaxError("Invalid characters in JSON");
}
return eval('(' + str + ')');
Expand Down
1 change: 1 addition & 0 deletions tests/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ define(["doh/main", "../json", "../has"], function(doh, JSON, has){
function simpleNull(t){ t.is(null, JSON.parse('{"foo":null}').foo); },
function simpleNumber(t){ t.is(3.3, JSON.parse('{"foo":3.3}', true).foo); },
function strictString(t){ t.is("bar", JSON.parse('{"foo":"bar"}', true).foo); },
function strictEmptyString(t){ t.is("", JSON.parse('{"foo":""}', true).foo); },
function strictStringEsc(t){ t.is("b\n\t\"ar()", JSON.parse('{"foo":"b\\n\\t\\"ar()"}', true).foo); },
function strictTrue(t){ t.is(true, JSON.parse('{"foo":true}', true).foo); },
function strictFalse(t){ t.is(false, JSON.parse('{"foo":false}', true).foo); },
Expand Down

0 comments on commit 54f9a48

Please sign in to comment.