Skip to content

Commit

Permalink
Improvements in sanitizing invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Jan 16, 2016
1 parent 5a5c04e commit 8ec471b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
https://github.com/josdejong/jsoneditor


## not yet released, version 5.1.2

- Improvements in sanitizing invalid JSON.


## 2016-01-16, version 5.1.1

- Fixed #257: Improving error messages for enum errors failed when the
Expand Down
40 changes: 24 additions & 16 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,38 @@ exports.sanitize = function (jsString) {
function next() { return jsString.charAt(i + 1); }
function prev() { return jsString.charAt(i - 1); }

// test whether the last non-whitespace character was a brace-open '{'
function prevIsBrace() {
var ii = i - 1;
while (ii >= 0) {
var cc = jsString.charAt(ii);
if (cc === '{') {
return true;
}
else if (cc === ' ' || cc === '\n' || cc === '\r') { // whitespace
ii--;
}
else {
return false;
// get the last parsed non-whitespace character
function lastNonWhitespace () {
var p = chars.length - 1;

while (p >= 0) {
var pp = chars[p];
if (pp !== ' ' && pp !== '\n' && pp !== '\r' && pp !== '\t') { // non whitespace
return pp;
}
p--;
}
return false;

return '';
}

// skip a block comment '/* ... */'
function skipComment () {
function skipBlockComment () {
i += 2;
while (i < jsString.length && (curr() !== '*' || next() !== '/')) {
i++;
}
i += 2;
}

// skip a comment '// ...'
function skipComment () {
i += 2;
while (i < jsString.length && (curr() !== '\n')) {
i++;
}
}

// parse single or double quoted string
function parseString(quote) {
chars.push('"');
Expand Down Expand Up @@ -129,12 +134,15 @@ exports.sanitize = function (jsString) {
var c = curr();

if (c === '/' && next() === '*') {
skipBlockComment();
}
else if (c === '/' && next() === '/') {
skipComment();
}
else if (c === '\'' || c === '"') {
parseString(c);
}
else if (/[a-zA-Z_$]/.test(c) && prevIsBrace()) {
else if (/[a-zA-Z_$]/.test(c) && ['{', ','].indexOf(lastNonWhitespace()) !== -1) {
// an unquoted object key (like a in '{a:2}')
parseKey();
}
Expand Down
4 changes: 4 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('util', function () {
assert.equal(util.sanitize('{a:2}'), '{"a":2}');
assert.equal(util.sanitize('{\'a\':2}'), '{"a":2}');
assert.equal(util.sanitize('{a:\'foo\'}'), '{"a":"foo"}');
assert.equal(util.sanitize('{a:\'foo\',b:\'bar\'}'), '{"a":"foo","b":"bar"}');

// should leave string content untouched
assert.equal(util.sanitize('"{a:b}"'), '"{a:b}"');
Expand All @@ -28,6 +29,9 @@ describe('util', function () {

it('remove comments', function () {
assert.equal(util.sanitize('/* foo */ {}'), ' {}');
assert.equal(util.sanitize('/* foo */ {}'), ' {}');
assert.equal(util.sanitize('{a:\'foo\',/*hello*/b:\'bar\'}'), '{"a":"foo","b":"bar"}');
assert.equal(util.sanitize('{\na:\'foo\',//hello\nb:\'bar\'\n}'), '{\n"a":"foo",\n"b":"bar"\n}');

// should not remove comments in string
assert.equal(util.sanitize('{"str":"/* foo */"}'), '{"str":"/* foo */"}');
Expand Down

0 comments on commit 8ec471b

Please sign in to comment.