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,
|
- Attributes on arrays, maps, sets, and functions,
|
||||||
- Function closures.
|
- 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
|
Note that this a strict superset of JSON, this if only JSON-supported
|
||||||
data is serialized the output will be strict JSON.
|
data is serialized the output will be strict JSON.
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-serialize",
|
"name": "ig-serialize",
|
||||||
"version": "1.3.2",
|
"version": "1.3.3",
|
||||||
"description": "experimental extended json serializaion...",
|
"description": "experimental extended json serializaion...",
|
||||||
"main": "serialize.js",
|
"main": "serialize.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
29
serialize.js
29
serialize.js
@ -53,7 +53,6 @@
|
|||||||
(function(require){ var module={} // make module AMD/node compatible...
|
(function(require){ var module={} // make module AMD/node compatible...
|
||||||
/*********************************************************************/
|
/*********************************************************************/
|
||||||
|
|
||||||
|
|
||||||
var NULL = 'null'
|
var NULL = 'null'
|
||||||
var UNDEFINED = 'undefined'
|
var UNDEFINED = 'undefined'
|
||||||
var NAN = 'NaN'
|
var NAN = 'NaN'
|
||||||
@ -333,10 +332,8 @@ module.eJSON = {
|
|||||||
undefined: undefined,
|
undefined: undefined,
|
||||||
NaN: NaN,
|
NaN: NaN,
|
||||||
|
|
||||||
//'<REF': 'reference',
|
|
||||||
[REFERENCE.split('%')[0]]: 'reference',
|
[REFERENCE.split('%')[0]]: 'reference',
|
||||||
|
|
||||||
//'<FUNC[': 'func',
|
|
||||||
[FUNCTION.split('%')[0]]: 'func',
|
[FUNCTION.split('%')[0]]: 'func',
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -445,19 +442,39 @@ module.eJSON = {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE: this treats comments as whitespace...
|
||||||
|
COMMENTS: true,
|
||||||
WHITESPACE: ' \t\n',
|
WHITESPACE: ' \t\n',
|
||||||
skipWhitespace: function(str, i, line){
|
skipWhitespace: function(str, i, line){
|
||||||
while(i < str.length
|
while(i < str.length){
|
||||||
&& this.WHITESPACE.includes(str[i])){
|
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'){
|
if(str[i] == '\n'){
|
||||||
line++ }
|
line++ }
|
||||||
i++ }
|
i++ }
|
||||||
return [i, line] },
|
return [i, line] },
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// .handler(match, str, i, line)
|
// .handler(state, path, match, str, i, line)
|
||||||
// -> [value, i, line]
|
// -> [value, i, line]
|
||||||
//
|
//
|
||||||
|
|
||||||
number: function(state, path, match, str, i, line){
|
number: function(state, path, match, str, i, line){
|
||||||
debug.lex('number', str, i, line)
|
debug.lex('number', str, i, line)
|
||||||
// special cases..,
|
// special cases..,
|
||||||
|
|||||||
22
test.js
22
test.js
@ -28,6 +28,10 @@ var ejson = false
|
|||||||
|
|
||||||
var pre_cycle = true
|
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...
|
// XXX test whitespace handling...
|
||||||
var setups = test.Setups({
|
var setups = test.Setups({
|
||||||
'true': function(assert){
|
'true': function(assert){
|
||||||
@ -228,6 +232,24 @@ test.Cases({
|
|||||||
|
|
||||||
// arrays...
|
// arrays...
|
||||||
['[1,2,]', '[1,2]'],
|
['[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){
|
'syntax-simplifications': function(assert){
|
||||||
var aa, bb
|
var aa, bb
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user