Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 34 additions & 41 deletions dist/json_parser.js

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

61 changes: 61 additions & 0 deletions lib/backslash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict';

function backslash(str, pos = 0) {
let escaped = false;
const ret = [];
while (pos < str.length) {
const c = str[pos++];
if (escaped) {
escaped = false;
switch (c) {
case 'n':
ret.push('\n');
continue;
case 'r':
ret.push('\r');
continue;
case 'f':
ret.push('\f');
continue;
case 'b':
ret.push('\b');
continue;
case 't':
ret.push('\t');
continue;
case 'v':
ret.push('\v');
continue;
case '\\':
ret.push('\\') ;
continue;
case '"':
ret.push('"') ;
continue;
case '\'':
ret.push('\'') ;
continue;
case 'u':
const num = str.slice(pos, pos + 4);
if(num.match(/^[a-fA-F0-9]{4}$/)){
ret.push(String.fromCharCode(parseInt(num, 16)));
pos += 4;
continue;
}
ret.push('\\u');
continue;
default:
ret.push('\\' + c);
continue;
}
}
if (c === '\\') {
escaped = true;
continue;
}
ret.push(c);
}
return ret.join('');
}

module.exports = backslash;
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "this repo support parse standard [json](http://json.org/) and something like this:",
"main": "dist/json_parser.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha test/main.test.js --require babel-register",
"build": "jison src/index.jison -o dist/json_parser.js"
},
"repository": {
"type": "git",
Expand All @@ -15,9 +16,6 @@
"bugs": {
"url": "https://github.com/albin3/editor-json-parser/issues"
},
"scripts": {
"test": "mocha test/main.test.js --require babel-register"
},
"homepage": "https://github.com/albin3/editor-json-parser#readme",
"devDependencies": {
"babel-preset-es2015": "^6.22.0",
Expand Down
15 changes: 5 additions & 10 deletions src/index.jison
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ frac "."[0-9]+
[\/\/].*\n /* skip comments */

{int}{frac}?{exp}?\b return 'NUMBER'
\"(?:'\\'[\\"bfnrt/]|'\\u'[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*\" yytext = yytext.substr(1,yyleng-2); return 'STRING'
\'(?:'\\'[\\"bfnrt/]|'\\u'[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f'])*\' yytext = yytext.substr(1,yyleng-2); return 'STRING'
\"(?:\\[\\"]|[^\0-\x09\x0a-\x1f"])*\" yytext = yytext.substr(1,yyleng-2); return 'STRING'
\'(?:\\[\\']|[^\0-\x09\x0a-\x1f'])*\' yytext = yytext.substr(1,yyleng-2); return 'STRING'

"{" return '{'
"}" return '}'
Expand Down Expand Up @@ -38,19 +38,12 @@ frac "."[0-9]+
ECMA-262 5th Edition, 15.12.1 The JSON Grammar.
*/


%%

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');
$$ = backslash(yytext);
}
;

Expand Down Expand Up @@ -131,3 +124,5 @@ JSONElementList
{$$ = $1; $1.push($3);}
;

%%
const backslash = require('../lib/backslash');
48 changes: 48 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,54 @@ describe ('json parser test.', function () {

result.should.deepEqual({a: 'xxx'});
})

it ('support { a: "x\\啊xx"}', function () {
let result = jsonParser.parse(`
{ a: "x\\啊xx"}
`);

result.should.deepEqual({a: 'x\\啊xx'});
})

it ('support { a: "x\\\\nxx"}', function () {
let result = jsonParser.parse(`
{ a: "x\\\\nxx"}
`);

result.should.deepEqual({a: 'x\\nxx'});
})

it ('support { a: "x\\u1234xxx"}', function () {
let result = jsonParser.parse(`
{ a: "x\\u1234xxx"}
`);

result.should.deepEqual({a: 'x\u1234xxx'});
})

it ('support { a: "x\\u123xxx"}', function () {
let result = jsonParser.parse(`
{ a: "x\\u123xxx"}
`);

result.should.deepEqual({a: 'x\\u123xxx'});
})

it ('support { a: "x\\\\"}', function () {
let result = jsonParser.parse(`
{ a: "x\\\\"}
`);

result.should.deepEqual({a: 'x\\'});
})

it ('support { a: "x\\""}', function () {
let result = jsonParser.parse(`
{ a: "x\\""}
`);

result.should.deepEqual({a: 'x"'});
})
});