mirror of
https://github.com/flynx/serialize.js.git
synced 2026-08-25 17:26:42 +00:00
added comment support...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
1bdca62df9
commit
81839f8b24
@ -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.
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-serialize",
|
||||
"version": "1.3.2",
|
||||
"version": "1.3.3",
|
||||
"description": "experimental extended json serializaion...",
|
||||
"main": "serialize.js",
|
||||
"scripts": {
|
||||
|
||||
31
serialize.js
31
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,
|
||||
|
||||
//'<REF': 'reference',
|
||||
[REFERENCE.split('%')[0]]: 'reference',
|
||||
|
||||
//'<FUNC[': 'func',
|
||||
[FUNCTION.split('%')[0]]: 'func',
|
||||
},
|
||||
|
||||
|
||||
|
||||
// generic helpers...
|
||||
//
|
||||
@ -445,19 +442,39 @@ module.eJSON = {
|
||||
|
||||
|
||||
|
||||
// NOTE: this treats comments as whitespace...
|
||||
COMMENTS: true,
|
||||
WHITESPACE: ' \t\n',
|
||||
skipWhitespace: function(str, i, line){
|
||||
while(i < str.length
|
||||
&& this.WHITESPACE.includes(str[i])){
|
||||
while(i < str.length){
|
||||
if(this.COMMENTS){
|
||||
// comment: '// ... \n'
|
||||
if(str[i] == '/' && str[i+1] == '/'){
|
||||
while(i < str.length && str[i] != '\n'){
|
||||
i++ } }
|
||||
// comment: '/* ... */'
|
||||
if(str[i] == '/' && str[i+1] == '*'){
|
||||
while(i < str.length && str.slice(i, i+2) != '*/'){
|
||||
if(str[i] == '\n'){
|
||||
line++ }
|
||||
i++ }
|
||||
i += 2
|
||||
if(i > 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..,
|
||||
|
||||
22
test.js
22
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user