Skip to content

Commit

Permalink
apply Fix string escapes. (issue zaach#37)
Browse files Browse the repository at this point in the history
zaach#54

from ntdaley:master with commits:
#ecf1830f21634f2b711b4fd840789ec8ddf01649
#aaf81b140f12cfa20ba9411770fa26f665ba6010
  • Loading branch information
russaa committed Jun 24, 2020
1 parent 68f8659 commit 3323743
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ function parse(source) {
}
}

return JSON.stringify(parsed, null, options.indent);
return JSON.stringify(parsed, null, options.indent).replace(/[^\n\r\ \x20-\x7e]/g, function(char) {
return '\\u' + ('0000' + Number(char.charCodeAt(0)).toString(16)).match(/....$/)[0];
});
} catch (e) {
if (options.forcePrettyPrint) {
/* From https://github.com/umbrae/jsonlintdotcom:
Expand Down
20 changes: 13 additions & 7 deletions src/jsonlint.y
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ _p.setStrict = function(isEnabled){
JSONString
: STRING
{ // replace escaped characters with actual character
$$ = yytext.replace(/\\(\\|")/g, "$"+"1")
.replace(/\\n/g,'\n')
.replace(/\\r/g,'\r')
.replace(/\\t/g,'\t')
.replace(/\\v/g,'\v')
.replace(/\\f/g,'\f')
.replace(/\\b/g,'\b');
$$ = yytext.replace(/\\([\"\\\/bfnrt]|u[0-9a-fA-f]{4})/g, function(match, part) {
if(part.charAt(0) === 'u') {
return String.fromCharCode(parseInt(part.substr(1),16));
}
switch(part) {
case 'b':return '\b';
case 'f':return '\f';
case 'n':return '\n';
case 'r':return '\r';
case 't':return '\t';
case '"':case '\\':case '/':return part;
}
});
}
;

Expand Down
18 changes: 18 additions & 0 deletions test/all-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,35 @@ exports["test escaped chars"] = function () {
assert.deepEqual(parser.parse(json), {"foo": '\\\"'});
};

exports["test all escaped characters"] = function () {
var json = '["\\u20AC","\\/","\\\\","\\b","\\f","\\n","\\r","\\t","\\\""]';
assert.deepEqual(parser.parse(json), ['\u20AC','\/','\\','\b','\f','\n','\r','\t','\"']);
};

exports["test escaped \\n"] = function () {
var json = '{"foo": "\\\\\\n"}';
assert.deepEqual(parser.parse(json), {"foo": '\\\n'});
};

exports["test escaped backslash does not get used to escape"] = function () {
var json = '{"foo": "\\\\n"}';
assert.deepEqual(parser.parse(json), {"foo": '\\n'});
};

exports["test string with escaped line break"] = function () {
var json = '{"foo": "bar\\nbar"}';
assert.deepEqual(parser.parse(json), {"foo": "bar\nbar"});
assert.equal(JSON.stringify(parser.parse(json)).length, 18);
};

exports["test escaped value and key chars"] = function () {
var json = fs.readFileSync(__dirname + "/passes/4.json").toString();
assert.deepEqual(parser.parse(json), {
hex: '\u0123\u4567\u89AB\uCDEF\uabcd\uef4A',
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?": 'special char key'
});
};

exports["test string with line break"] = function () {
var json = '{"foo": "bar\nbar"}';
assert["throws"](function () {parser.parse(json)}, "should throw error");
Expand Down
4 changes: 4 additions & 0 deletions test/passes/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "special char key"
}

0 comments on commit 3323743

Please sign in to comment.