2026-01-01 12:57:24 +03:00
|
|
|
# serilize.js: Extended JSON serilization
|
|
|
|
|
|
|
|
|
|
This extends the default JSON serialization adding the following:
|
|
|
|
|
- Recursive data structure serialization
|
|
|
|
|
- `undefined`/`NaN` serialization
|
2026-01-16 15:52:22 +03:00
|
|
|
- Serialization of `BigInt`'s, `Set`'s, `Map`'s
|
2026-01-01 12:57:24 +03:00
|
|
|
- Function serialization (off by default)
|
|
|
|
|
- Deep and partial-deep cleen object copy
|
|
|
|
|
|
2026-01-14 18:18:06 +03:00
|
|
|
Possible differences to JSON output:
|
2026-01-16 16:21:59 +03:00
|
|
|
- Repeating long strings and BigInts are referenced by default instead of
|
|
|
|
|
being reincluded in the output.
|
|
|
|
|
|
2026-01-01 12:57:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
## Motivation
|
|
|
|
|
|
2026-01-01 23:56:20 +03:00
|
|
|
This was originally built as a companion to a testing module for a
|
|
|
|
|
programming class, illustrating several concepts, including: guaranteed
|
|
|
|
|
clean isolation of data structures via serialization, instrumenting code
|
|
|
|
|
and tooling design, basic parsing, among others.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
2026-01-14 18:04:57 +03:00
|
|
|
```shell
|
|
|
|
|
$ npm install ig-serilaize
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Or just download and drop [serialize.js](serialize.js) into your code.
|
|
|
|
|
|
|
|
|
|
|
2026-01-01 23:56:20 +03:00
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Serializing functions
|
|
|
|
|
|
|
|
|
|
Due to how JavaScript is designed it is not possible to trivially and
|
|
|
|
|
fully clone a function with all of it's references, `.serilaize(..)` will
|
|
|
|
|
not attempt to clone any state a function may have, this will lead to
|
|
|
|
|
loosing:
|
|
|
|
|
|
|
|
|
|
- Function closure
|
|
|
|
|
- Attributes set on the function or any of it's prototypes, including the
|
|
|
|
|
`.__proto__` value if it was changed.
|
|
|
|
|
|
|
|
|
|
Thus, care must be taken when serializing structures containing function.
|
|
|
|
|
|
|
|
|
|
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-01 23:56:20 +03:00
|
|
|
## API
|
|
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
### `serialize(..)` / `eJSON.stringify(..)`
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-15 15:24:05 +03:00
|
|
|
Serialize a JavaScript value into a JSON/eJSON string.
|
2026-01-15 15:07:37 +03:00
|
|
|
```
|
|
|
|
|
serialize(<value>)
|
|
|
|
|
eJSON.stringify(<value>)
|
|
|
|
|
-> <string>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
More control:
|
|
|
|
|
```
|
|
|
|
|
serialize(obj, options){
|
|
|
|
|
serialize(obj, indent, depth=0, options){
|
|
|
|
|
-> <string>
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-16 16:21:59 +03:00
|
|
|
Options format:
|
|
|
|
|
```
|
|
|
|
|
{
|
|
|
|
|
// pretty-printing indent...
|
|
|
|
|
// (default: undefined)
|
|
|
|
|
indent: undefined,
|
|
|
|
|
|
|
|
|
|
// outout root indent...
|
|
|
|
|
// (default: 0)
|
|
|
|
|
depth: 0,
|
|
|
|
|
|
|
|
|
|
// minimal referenced string/bigint length...
|
|
|
|
|
// (default: MIN_LENGTH_REF)
|
|
|
|
|
min_length_ref: MIN_LENGTH_REF,
|
|
|
|
|
|
|
|
|
|
// functions list...
|
|
|
|
|
// (default: undefined)
|
|
|
|
|
functions: undefined,
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-15 15:07:37 +03:00
|
|
|
Supported options:
|
|
|
|
|
- `indent` controls formatting and nested value indent, if set to a number
|
|
|
|
|
that number of spaces will be used to indent nested values if given a
|
|
|
|
|
string that string is used for indenting, note that only whitespace is
|
|
|
|
|
supported currently.
|
|
|
|
|
Default: `undefined` (disabled)
|
|
|
|
|
- `depth` if given is a number of `indent`'s, used to set top level indent
|
|
|
|
|
depth of the returned string, this can be useful when pretty-printing
|
|
|
|
|
or nesting the output.
|
|
|
|
|
Default: `0`
|
|
|
|
|
- `min_length_ref` sets the minimal length of a string or big-int value
|
|
|
|
|
for referencing when encountered repeatedly.
|
|
|
|
|
If set to `0` or `Infinity` referencing of strings and big-ints will
|
|
|
|
|
be is disabled.
|
|
|
|
|
Default: 'MIN_LENGTH_REF'
|
|
|
|
|
- `functions` if passed an array, encounterd functions will be pushed to
|
|
|
|
|
it and stored in the output by index.
|
|
|
|
|
Default: `undefined`
|
|
|
|
|
|
|
|
|
|
|
2026-01-15 15:39:51 +03:00
|
|
|
### `deserialize(..)` / `eJSON.parse(..)`
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-15 15:24:05 +03:00
|
|
|
Deserialize a JSON/eJSON into a value.
|
2026-01-15 15:07:37 +03:00
|
|
|
```
|
|
|
|
|
deserialize(<string>)
|
|
|
|
|
eJSON.parse(<string>)
|
|
|
|
|
-> <value>
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-15 15:24:05 +03:00
|
|
|
Deserializing function is disabled by default as it can be a security
|
|
|
|
|
risk if the eJSON came from an untrusted source.
|
|
|
|
|
|
|
|
|
|
Enable function deserialization:
|
|
|
|
|
```
|
|
|
|
|
deserialize(<string>, true)
|
|
|
|
|
eJSON.parse(<string>, true)
|
|
|
|
|
deserialize(<string>, {functions: true})
|
|
|
|
|
eJSON.parse(<string>, {functions: true})
|
|
|
|
|
-> <value>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Passing a function list (generated by `serialize(<value>, {functions: <functions>})`)
|
|
|
|
|
for deserialization:
|
|
|
|
|
```
|
|
|
|
|
deserialize(<string>, {functions: <functions>})
|
|
|
|
|
eJSON.parse(<string>, {functions: <functions>})
|
|
|
|
|
-> <value>
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-15 15:07:37 +03:00
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
### `deepCopy(..)`
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-15 15:07:37 +03:00
|
|
|
```
|
|
|
|
|
deepCopy(<value>)
|
|
|
|
|
-> <value>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
### `partialDeepCopy(..)`
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-15 15:07:37 +03:00
|
|
|
```
|
|
|
|
|
partialDeepCopy(<value>)
|
|
|
|
|
-> <value>
|
|
|
|
|
```
|
|
|
|
|
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
### `MIN_LENGTH_REF` / `<options>.min_length_ref`
|
2026-01-14 18:18:06 +03:00
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
Defines the default minimum length of repeating string or bin-int to
|
|
|
|
|
include as a reference in the output.
|
2026-01-01 23:56:20 +03:00
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
If set to `0`, referencing will be disabled.
|
2026-01-01 23:56:20 +03:00
|
|
|
|
2026-01-14 18:28:56 +03:00
|
|
|
Default: 96
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### `DEBUG`
|
2026-01-01 23:56:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-09 15:39:24 +03:00
|
|
|
## Format
|
|
|
|
|
|
2026-01-11 03:51:26 +03:00
|
|
|
The output of `.serialize(..)` is a strict superset of [standard JSON](https://www.json.org/json-en.html),
|
2026-01-10 03:44:59 +03:00
|
|
|
while the input format is a bit more relaxed than in several details.
|
2026-01-09 15:39:24 +03:00
|
|
|
|
|
|
|
|
Extensions to JSON:
|
|
|
|
|
- Recursion
|
2026-01-11 03:51:26 +03:00
|
|
|
- undefined / NaN
|
2026-01-09 15:39:24 +03:00
|
|
|
- BigInt
|
|
|
|
|
- Map / Set
|
2026-01-10 03:44:59 +03:00
|
|
|
- Function
|
|
|
|
|
|
|
|
|
|
### Structural paths
|
|
|
|
|
|
|
|
|
|
### Recursion
|
|
|
|
|
|
2026-01-13 03:20:11 +03:00
|
|
|
If an object is encountered
|
|
|
|
|
|
2026-01-10 03:44:59 +03:00
|
|
|
### null types
|
|
|
|
|
|
|
|
|
|
### BigInt
|
|
|
|
|
|
|
|
|
|
### Map / Set
|
|
|
|
|
|
|
|
|
|
### Functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-01 12:57:24 +03:00
|
|
|
|
|
|
|
|
|