any-serialize
Serialize / Deserialize any JavaScript objects, as long as you provides how-to. I have already provided Date
, RegExp
, Set
and Function
.
This package intentionally has no dependencies, and can be used safely in browser.
- Can also generate hash from anything, due to ability to serialize anything to String. (See
Serialize#hash
). - Cloning (See
Serialize#clone
) - Deep equality due to your definition (See
Serialize#deepEqual
)
Usage
See /tests.
Date
, RegExp
and Function
is already serialable via this library by default.
- Functions are only provided in "write-only mode".
- To enable "reconstruction", use
FullFunctionAdapter
. (Which is in principle similar to unsafe eval.)
- To enable "reconstruction", use
- Classes are not. You need to provide at least
static fromJSON()
andtoJSON()
- You can provide a custom way to serialize, including the forementioned
Date
,RegExp
andFunction
.
/** * You can turn off prefix with __prefix__ = '' */ // static __prefix__ = '' static __key__ = 'unsafeName' static { const a b = arg return a b } a: number b: number { thisa = a thisb = b } { const a b = this return a b } cconst ser = ser const r = ser console// {"a":{"__Date":"2020-02-18T17:53:37.557Z"},"r":{"__RegExp":{"source":"^hello ","flags":"gi"}},"c":{"__unsafeName":{"a":1,"b":3}},"f":{"__Function":"(a, b) => a + b"}} const s = serconsole// { a: 2020-02-18T17:52:02.555Z,// r: /^hello /gi,// c: CustomClass1 { a: 1, b: 3 },// f: [Function: anonymous] }console// {"a":{"__Date":"2020-02-18T17:53:37.557Z"},"r":{"__RegExp":{"source":"^hello ","flags":"gi"}},"c":{"__unsafeName":{"a":1,"b":3}},"f":{"__Function":"(a, b) => a + b"}}