mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 03:00:57 +00:00
made the output more compact...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
d9b1bce502
commit
58d09563aa
48
README.md
48
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
|
||||
<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
|
||||
|
||||
### Infinity
|
||||
|
||||
### Map / Set
|
||||
|
||||
### Functions
|
||||
@ -285,4 +309,4 @@ $ node ./test.js
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- vim:set nowrap : -->
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-serialize",
|
||||
"version": "1.2.1",
|
||||
"version": "1.3.0",
|
||||
"description": "experimental extended json serializaion...",
|
||||
"main": "serialize.js",
|
||||
"scripts": {
|
||||
|
||||
10
serialize.js
10
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 = '])'
|
||||
|
||||
8
test.js
8
test.js
@ -82,13 +82,13 @@ var setups = test.Setups({
|
||||
'array-empty': function(assert){
|
||||
return ['[]', json] },
|
||||
'array-sparse-a': function(assert){
|
||||
return ['[<empty>]'] },
|
||||
return ['[,]'] },
|
||||
'array-sparse-b': function(assert){
|
||||
return ['["a",<empty>]'] },
|
||||
return ['["a",,]'] },
|
||||
'array-sparse-c': function(assert){
|
||||
return ['[<empty>,"a"]'] },
|
||||
return ['[,"a"]'] },
|
||||
'array-sparse-d': function(assert){
|
||||
return ['[<empty>,1,<empty>,<empty>,2,<empty>]'] },
|
||||
return ['[,1,,,2,,]'] },
|
||||
|
||||
'object-empty': function(assert){
|
||||
return ['{}', json] },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user