diff --git a/README.md b/README.md index 7cb535e..df7428e 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ Data not stored: - Attributes on arrays, maps, sets, and functions, - Function closures. +Style and input sanitization: +- Comments are treated as whitespace and are stripped from input +- Trailing commas are ignored + Note that this a strict superset of JSON, this if only JSON-supported data is serialized the output will be strict JSON. diff --git a/package.json b/package.json index 9575edb..8af2cda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-serialize", - "version": "1.3.2", + "version": "1.3.3", "description": "experimental extended json serializaion...", "main": "serialize.js", "scripts": { diff --git a/serialize.js b/serialize.js index 76a1d0e..744989f 100644 --- a/serialize.js +++ b/serialize.js @@ -53,7 +53,6 @@ (function(require){ var module={} // make module AMD/node compatible... /*********************************************************************/ - var NULL = 'null' var UNDEFINED = 'undefined' var NAN = 'NaN' @@ -333,13 +332,11 @@ module.eJSON = { undefined: undefined, NaN: NaN, - //' str.length){ + this.error('Unexpected end of input wile looking for "*/".', str, i-2, line) } } } + // non-whitespace... + if(!this.WHITESPACE.includes(str[i])){ + break } if(str[i] == '\n'){ line++ } i++ } return [i, line] }, + // - // .handler(match, str, i, line) + // .handler(state, path, match, str, i, line) // -> [value, i, line] // + number: function(state, path, match, str, i, line){ debug.lex('number', str, i, line) // special cases.., diff --git a/test.js b/test.js index 5554437..5c7756b 100755 --- a/test.js +++ b/test.js @@ -28,6 +28,10 @@ var ejson = false var pre_cycle = true +// XXX BUG: deserialize('1 2') -> 1 +// should throw: +// 'SyntaxError: Unexpected non-whitespace character after JSON at position 2 (line 1 column 3)' + // XXX test whitespace handling... var setups = test.Setups({ 'true': function(assert){ @@ -228,6 +232,24 @@ test.Cases({ // arrays... ['[1,2,]', '[1,2]'], + + // comments... + ['123 // comment...', '123'], + ['// comment...\n123', '123'], + ['// comment...\n123// comment...', '123'], + ['/* comment */ 123', '123'], + ['123 /* comment */', '123'], + ['/* comment */123/* comment */', '123'], + [`// comment... + [ + // comment... + 1, // comment... + 2 /* comment */, + // comment... + /* comment */ 3 /* comment*/, + // comment... + ] + // comment...`, '[1,2,3]'] ], 'syntax-simplifications': function(assert){ var aa, bb