From 58d09563aa88a99fce9a138449635393a8c75ce3 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 17 Jan 2026 15:21:47 +0300 Subject: [PATCH] made the output more compact... Signed-off-by: Alex A. Naanou --- README.md | 48 ++++++++++++++++++++++++++++++++++++------------ package.json | 2 +- serialize.js | 10 ++++++---- test.js | 8 ++++---- 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8b26f3e..bd74543 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ This extends the default JSON serialization adding the following: - Recursive data structure serialization +- Sparse array serialization - `undefined`/`NaN` serialization -- Serialization of `BigInt`'s, `Set`'s, `Map`'s +- Serialization of `Infinity`, `BigInt`'s, `Set`'s, `Map`'s - Function serialization (off by default) - Deep and partial-deep cleen object copy @@ -179,12 +180,6 @@ Default: 96 The output of `.serialize(..)` is a strict superset of [standard JSON](https://www.json.org/json-en.html), while the input format is a bit more relaxed than in several details. -Extensions to JSON: -- References -- undefined / NaN -- BigInt -- Map / Set -- Function ### Structural paths @@ -202,7 +197,7 @@ structure traversed: Note that string path items are unambiguous and are always treated as attributes. -For examples see next section. +An empty path indicates the root object. ### Referencing @@ -210,8 +205,6 @@ For examples see next section. If an object is encountered for a second time it will be serialized as a reference by path to the first occurrence. -An empty path indicates the root object. - Format: ```bnf ::= @@ -254,10 +247,41 @@ serialize(new Map([['key', o]])) // -> 'Map([["key",[]]])' ``` -### null types +### Null types + +In addition to `null`, serialize.js adds support for `undefined` and +`NaN` which are stored as-is. + +Example: +```javascript +serialize([null, undefined, NaN]) // -> '[null,undefined,NaN]' +``` + +### Sparse arrays + +Sparse arrays are represented in the same way JavaScript handles them +syntactically. + +Example: +```javascript +serialize([,]) // -> '[,]' +serialize([,,]) // -> '[,,]' +``` + +Note that trailing commas are ignored in the same way JavaScript ignores +them: +```javascript +// trailing comma... +serialize([1,]) // -> '[1]' + +// sparse element... +serialize([1,,]) // -> '[1,,]' +``` ### BigInt +### Infinity + ### Map / Set ### Functions @@ -285,4 +309,4 @@ $ node ./test.js - + diff --git a/package.json b/package.json index b389b55..9369cb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-serialize", - "version": "1.2.1", + "version": "1.3.0", "description": "experimental extended json serializaion...", "main": "serialize.js", "scripts": { diff --git a/serialize.js b/serialize.js index 3dba9a0..7c72af3 100644 --- a/serialize.js +++ b/serialize.js @@ -247,11 +247,13 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){ if(obj instanceof Array){ pre = '[' post = ']' + elems.length = obj.length for(var i=0; i < obj.length; i++){ - elems.push( - i in obj ? - _serialize(obj[i], [...path, i], seen, indent, depth+1, options) - : EMPTY) } + if(i in obj){ + elems[i] = _serialize(obj[i], [...path, i], seen, indent, depth+1, options) } } + // add trailing coma if last elem is empty... + if(!(elems.length-1 in elems)){ + elems.length++ } } else if(obj instanceof Map){ pre = 'Map([' post = '])' diff --git a/test.js b/test.js index f4ceab6..ed9fac6 100755 --- a/test.js +++ b/test.js @@ -82,13 +82,13 @@ var setups = test.Setups({ 'array-empty': function(assert){ return ['[]', json] }, 'array-sparse-a': function(assert){ - return ['[]'] }, + return ['[,]'] }, 'array-sparse-b': function(assert){ - return ['["a",]'] }, + return ['["a",,]'] }, 'array-sparse-c': function(assert){ - return ['[,"a"]'] }, + return ['[,"a"]'] }, 'array-sparse-d': function(assert){ - return ['[,1,,,2,]'] }, + return ['[,1,,,2,,]'] }, 'object-empty': function(assert){ return ['{}', json] },