added comment support...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2026-06-11 16:11:42 +03:00
parent 1bdca62df9
commit 81839f8b24
4 changed files with 51 additions and 8 deletions

View File

@ -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.

View File

@ -1,6 +1,6 @@
{
"name": "ig-serialize",
"version": "1.3.2",
"version": "1.3.3",
"description": "experimental extended json serializaion...",
"main": "serialize.js",
"scripts": {

View File

@ -53,7 +53,6 @@
(function(require){ var module={} // make module AMD/node compatible...
/*********************************************************************/
var NULL = 'null'
var UNDEFINED = 'undefined'
var NAN = 'NaN'
@ -333,10 +332,8 @@ module.eJSON = {
undefined: undefined,
NaN: NaN,
//'<REF': 'reference',
[REFERENCE.split('%')[0]]: 'reference',
//'<FUNC[': 'func',
[FUNCTION.split('%')[0]]: 'func',
},
@ -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
View File

@ -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