mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 11:10:57 +00:00
docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
2fb5bc882d
commit
9a35039e68
52
README.md
52
README.md
@ -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`
|
||||||
|
|
||||||
|
|||||||
16
serialize.js
16
serialize.js
@ -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) }
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user