Skip to content

Commit ecf1830

Browse files
committed
Fix string escapes. (issue zaach#37)
Previously the different escape characters were being applied one after the other, so that "\\n" in the json will get turned into a new-line instead of "\n". Previously unicode escapes were not being converted resulting in stringifying the parse result turning "\u20AC" getting turned into "\\u20AC". Previously "\/" was not being converted resulting in stringifying the parse result turning it into "\\/" Note: I removed handling of '\v' because this would not be accepted by the lexer anyway, and is not part of the json standard. Note: Because unicode escapes are converted, strings in the input like "\u20AC" will become their unicode equivalent after parsing (e.g. in this case the euro character). Also changed the command line use of JSON.stringify to further process the result to convert non-ASCII printable characters to unicode escapes. While not strictly necessary according to the JSON standard, ascii output is safer for some parsers, and now that the parser processes unicode escapes there is more chance of having non-ASCII characters in the parser output. I would suggest that it would be better to always use formatter.js instead of JSON.stringify, because that way the choice between unicode escaped values and unicode characters would always be the same for input and output. Similar formatting changes should probably be made in the web version.
1 parent f5cb6e7 commit ecf1830

File tree

5 files changed

+526
-263
lines changed

5 files changed

+526
-263
lines changed

lib/cli.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ function parse (source) {
9090
}
9191
}
9292

93-
return JSON.stringify(parsed, null, options.indent);
93+
return JSON.stringify(parsed, null, options.indent).replace(/[^\x20-\x7e]/g, function(char) {
94+
return '\\u' + ('0000' + Number(char.charCodeAt(0)).toString(16)).match(/....$/)[0];
95+
});
9496
} catch (e) {
9597
if (options.forcePrettyPrint) {
9698
/* From https://github.com/umbrae/jsonlintdotcom:

0 commit comments

Comments
 (0)