Skip to content

Commit 3323743

Browse files
committed
apply Fix string escapes. (issue zaach#37)
zaach#54 from ntdaley:master with commits: #ecf1830f21634f2b711b4fd840789ec8ddf01649 #aaf81b140f12cfa20ba9411770fa26f665ba6010
1 parent 68f8659 commit 3323743

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

lib/cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ function parse(source) {
108108
}
109109
}
110110

111-
return JSON.stringify(parsed, null, options.indent);
111+
return JSON.stringify(parsed, null, options.indent).replace(/[^\n\r\ \x20-\x7e]/g, function(char) {
112+
return '\\u' + ('0000' + Number(char.charCodeAt(0)).toString(16)).match(/....$/)[0];
113+
});
112114
} catch (e) {
113115
if (options.forcePrettyPrint) {
114116
/* From https://github.com/umbrae/jsonlintdotcom:

src/jsonlint.y

+13-7
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,19 @@ _p.setStrict = function(isEnabled){
6262
JSONString
6363
: STRING
6464
{ // replace escaped characters with actual character
65-
$$ = yytext.replace(/\\(\\|")/g, "$"+"1")
66-
.replace(/\\n/g,'\n')
67-
.replace(/\\r/g,'\r')
68-
.replace(/\\t/g,'\t')
69-
.replace(/\\v/g,'\v')
70-
.replace(/\\f/g,'\f')
71-
.replace(/\\b/g,'\b');
65+
$$ = yytext.replace(/\\([\"\\\/bfnrt]|u[0-9a-fA-f]{4})/g, function(match, part) {
66+
if(part.charAt(0) === 'u') {
67+
return String.fromCharCode(parseInt(part.substr(1),16));
68+
}
69+
switch(part) {
70+
case 'b':return '\b';
71+
case 'f':return '\f';
72+
case 'n':return '\n';
73+
case 'r':return '\r';
74+
case 't':return '\t';
75+
case '"':case '\\':case '/':return part;
76+
}
77+
});
7278
}
7379
;
7480

test/all-tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,35 @@ exports["test escaped chars"] = function () {
1717
assert.deepEqual(parser.parse(json), {"foo": '\\\"'});
1818
};
1919

20+
exports["test all escaped characters"] = function () {
21+
var json = '["\\u20AC","\\/","\\\\","\\b","\\f","\\n","\\r","\\t","\\\""]';
22+
assert.deepEqual(parser.parse(json), ['\u20AC','\/','\\','\b','\f','\n','\r','\t','\"']);
23+
};
24+
2025
exports["test escaped \\n"] = function () {
2126
var json = '{"foo": "\\\\\\n"}';
2227
assert.deepEqual(parser.parse(json), {"foo": '\\\n'});
2328
};
2429

30+
exports["test escaped backslash does not get used to escape"] = function () {
31+
var json = '{"foo": "\\\\n"}';
32+
assert.deepEqual(parser.parse(json), {"foo": '\\n'});
33+
};
34+
2535
exports["test string with escaped line break"] = function () {
2636
var json = '{"foo": "bar\\nbar"}';
2737
assert.deepEqual(parser.parse(json), {"foo": "bar\nbar"});
2838
assert.equal(JSON.stringify(parser.parse(json)).length, 18);
2939
};
3040

41+
exports["test escaped value and key chars"] = function () {
42+
var json = fs.readFileSync(__dirname + "/passes/4.json").toString();
43+
assert.deepEqual(parser.parse(json), {
44+
hex: '\u0123\u4567\u89AB\uCDEF\uabcd\uef4A',
45+
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?": 'special char key'
46+
});
47+
};
48+
3149
exports["test string with line break"] = function () {
3250
var json = '{"foo": "bar\nbar"}';
3351
assert["throws"](function () {parser.parse(json)}, "should throw error");

test/passes/4.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
3+
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "special char key"
4+
}

0 commit comments

Comments
 (0)