Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2026-01-15 15:07:37 +03:00
parent 2fb5bc882d
commit 9a35039e68
2 changed files with 68 additions and 0 deletions

View File

@ -53,12 +53,64 @@ Thus, care must be taken when serializing structures containing function.
### `serialize(..)` / `eJSON.stringify(..)` ### `serialize(..)` / `eJSON.stringify(..)`
Serialize a JavaScript value into a JSON or JSON-like string.
```
serialize(<value>)
eJSON.stringify(<value>)
-> <string>
```
More control:
```
serialize(obj, options){
serialize(obj, indent, depth=0, options){
-> <string>
```
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`
### `deserialize(..)` / 'eJSON.parse(..)' ### `deserialize(..)` / 'eJSON.parse(..)'
```
deserialize(<string>)
eJSON.parse(<string>)
-> <value>
```
### `deepCopy(..)` ### `deepCopy(..)`
```
deepCopy(<value>)
-> <value>
```
### `partialDeepCopy(..)` ### `partialDeepCopy(..)`
```
partialDeepCopy(<value>)
-> <value>
```
### `MIN_LENGTH_REF` / `<options>.min_length_ref` ### `MIN_LENGTH_REF` / `<options>.min_length_ref`

View File

@ -93,6 +93,15 @@ module.MIN_LENGTH_REF = REFERENCE.length * 16
// _serialize(obj, base_path, seen, indent, depth, options) // _serialize(obj, base_path, seen, indent, depth, options)
// -> str // -> str
// //
// Options format:
// {
// // Indent to use
// indent: undefined,
// depth: 0,
// min_length_ref: MIN_LENGTH_REF,
// functions: undefined,
// }
//
// //
// Paths // Paths
// A path is a chain of indexes/keys leading to a specific object in // A path is a chain of indexes/keys leading to a specific object in
@ -144,6 +153,8 @@ module.MIN_LENGTH_REF = REFERENCE.length * 16
var _serialize = var _serialize =
module._serialize = module._serialize =
function(obj, path=[], seen=new Map(), indent, depth=0, options={}){ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
if(typeof(indent) == 'number'){
indent = ' '.repeat(indent) }
var min_length_ref = var min_length_ref =
options.min_length_ref options.min_length_ref
?? module.MIN_LENGTH_REF ?? module.MIN_LENGTH_REF
@ -260,6 +271,11 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
var serialize = var serialize =
module.serialize = module.serialize =
function(obj, indent, depth=0, options){ function(obj, indent, depth=0, options){
if(indent
&& typeof(indent) == 'object'){
options = indent
indent = options.indent
depth = options.depth ?? 0 }
return _serialize(obj, [], new Map(), indent, depth, options) } return _serialize(obj, [], new Map(), indent, depth, options) }