Skip to content

Commit

Permalink
fix escaping of special characters (issue zaach#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed May 28, 2012
1 parent c07fb3d commit 14a108f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
18 changes: 13 additions & 5 deletions lib/jsonlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {

var $0 = $$.length - 1;
switch (yystate) {
case 1:this.$ = yytext.replace(/\\\\/g, "\\");
case 1: // replace escaped characters with actual character
this.$ = 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');

break;
case 2:this.$ = Number(yytext);
break;
Expand Down Expand Up @@ -367,13 +375,13 @@ var YYSTATE=YY_START
switch($avoiding_name_collisions) {
case 0:/* skip whitespace */
break;
case 1:return 6;
case 1:return 6
break;
case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4;
case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4
break;
case 3: return 17
case 3:return 17
break;
case 4: return 18
case 4:return 18
break;
case 5:return 23
break;
Expand Down
2 changes: 1 addition & 1 deletion src/jsonlint.l
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ frac "."[0-9]+
\"(?:'\\'[\\"bfnrt/]|'\\u'[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*\" yytext = yytext.substr(1,yyleng-2); return 'STRING'

"{" return '{'
"}" return ''
"}" return '}'
"[" return '['
"]" return ']'
"," return ','
Expand Down
10 changes: 9 additions & 1 deletion src/jsonlint.y
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

JSONString
: STRING
{$$ = yytext.replace(/\\\\/g, "\\");}
{ // 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');
}
;

JSONNumber
Expand Down
13 changes: 12 additions & 1 deletion test/all-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ exports["test escaped backslash"] = function () {
assert.deepEqual(parser.parse(json), {"foo": "\\"});
};

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

exports["test escaped \\n"] = 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.deepEqual(parser.parse(json), {"foo": "bar\nbar"});
assert.equal(JSON.stringify(parser.parse(json)).length, 18);
};

exports["test string with line break"] = function () {
Expand Down
2 changes: 1 addition & 1 deletion web/jsonlint.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 14a108f

Please sign in to comment.