made the output more compact...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2026-01-17 15:21:47 +03:00
parent d9b1bce502
commit 58d09563aa
4 changed files with 47 additions and 21 deletions

View File

@ -2,8 +2,9 @@
This extends the default JSON serialization adding the following: This extends the default JSON serialization adding the following:
- Recursive data structure serialization - Recursive data structure serialization
- Sparse array serialization
- `undefined`/`NaN` 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) - Function serialization (off by default)
- Deep and partial-deep cleen object copy - 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), 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. 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 ### Structural paths
@ -202,7 +197,7 @@ structure traversed:
Note that string path items are unambiguous and are always treated as Note that string path items are unambiguous and are always treated as
attributes. attributes.
For examples see next section. An empty path indicates the root object.
### Referencing ### Referencing
@ -210,8 +205,6 @@ For examples see next section.
If an object is encountered for a second time it will be serialized as If an object is encountered for a second time it will be serialized as
a reference by path to the first occurrence. a reference by path to the first occurrence.
An empty path indicates the root object.
Format: Format:
```bnf ```bnf
<ref> ::= <ref> ::=
@ -254,10 +247,41 @@ serialize(new Map([['key', o]])) // -> 'Map([["key",[<REF[0,1]>]]])'
``` ```
### 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 ### BigInt
### Infinity
### Map / Set ### Map / Set
### Functions ### Functions
@ -285,4 +309,4 @@ $ node ./test.js
<!-- vim:set nowrap : -->

View File

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

View File

@ -247,11 +247,13 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
if(obj instanceof Array){ if(obj instanceof Array){
pre = '[' pre = '['
post = ']' post = ']'
elems.length = obj.length
for(var i=0; i < obj.length; i++){ for(var i=0; i < obj.length; i++){
elems.push( if(i in obj){
i in obj ? elems[i] = _serialize(obj[i], [...path, i], seen, indent, depth+1, options) } }
_serialize(obj[i], [...path, i], seen, indent, depth+1, options) // add trailing coma if last elem is empty...
: EMPTY) } if(!(elems.length-1 in elems)){
elems.length++ }
} else if(obj instanceof Map){ } else if(obj instanceof Map){
pre = 'Map([' pre = 'Map(['
post = '])' post = '])'

View File

@ -82,13 +82,13 @@ var setups = test.Setups({
'array-empty': function(assert){ 'array-empty': function(assert){
return ['[]', json] }, return ['[]', json] },
'array-sparse-a': function(assert){ 'array-sparse-a': function(assert){
return ['[<empty>]'] }, return ['[,]'] },
'array-sparse-b': function(assert){ 'array-sparse-b': function(assert){
return ['["a",<empty>]'] }, return ['["a",,]'] },
'array-sparse-c': function(assert){ 'array-sparse-c': function(assert){
return ['[<empty>,"a"]'] }, return ['[,"a"]'] },
'array-sparse-d': function(assert){ 'array-sparse-d': function(assert){
return ['[<empty>,1,<empty>,<empty>,2,<empty>]'] }, return ['[,1,,,2,,]'] },
'object-empty': function(assert){ 'object-empty': function(assert){
return ['{}', json] }, return ['{}', json] },