serialify
Extended object serializer/deserializer for Node.js and browser written in TypeScript.
Feature
- Minimal: input any supported object, and get serialized/deserialized.
- Fast: it's basically a recursive if statement.
- Tiny: only 1.7kb of gzipped.
Supported data types
serialify
can serialize many data types which is not supported by standard JSON!
type | standard JSON | serialify |
---|---|---|
number |
✅ | ✅ |
boolean |
✅ | ✅ |
string |
✅ | ✅ |
null |
✅ | ✅ |
Array |
✅ | ✅ |
Object |
✅ | ✅ |
undefined |
❌ | ✅ |
bigint |
❌ | ✅ |
NaN |
❌ | ✅ |
Infinity |
❌ | ✅ |
Date |
❌ | ✅ |
RegExp |
❌ | ✅ |
Set |
❌ | ✅ |
Map |
❌ | ✅ |
URL |
❌ | ✅ |
URLSearchParams |
❌ | ✅ |
Buffer |
❌ | ✅ |
DataView |
❌ | ✅ |
ArrayBuffer |
❌ | ✅ |
Int8Array |
❌ | ✅ |
Uint8Array |
❌ | ✅ |
Uint8ClampedArray |
❌ | ✅ |
Int16Array |
❌ | ✅ |
Uint16Array |
❌ | ✅ |
Int32Array |
❌ | ✅ |
Uint32Array |
❌ | ✅ |
Float32Array |
❌ | ✅ |
Float64Array |
❌ | ✅ |
BigInt64Array |
❌ | ✅ |
BigUint64Array |
❌ | ✅ |
Installation
npm install serialify
Usage
Serialize
serialize
The code above will produce the following serialized object:
typing
For TypeScript, serialize()
will also translate the type of the source object.
/** * s: { * name: string; * birth: { __t: 'Date', __v: number }; * } **/
Stringify
To get a json string representation of serialized object directly, just import and call stringify()
.
stringify
The code above will produce the following string:
'{ "String": "John", "Number": 28, "Set": { "__t": "Set", "__v": [0, 1, 2, 3] }, "Date": { "__t": "Date", "__v": 1600392736750 } }'
Deserialize
deserialize()
to make serialized object back into it's origin.
/** * serialized = { * String: 'John', * Number: 28, * Set: { __t: 'Set', __v: [0, 1, 2, 3] }, * Date: { __t: 'Date', __v: 1600392736750 } * } **/ deserializeserialized /** * { * String: 'John', * Number: 28, * Set: Set([0, 1, 2, 3]), * Date: Date(1600392736750), * } **/
typing
deserialize()
will also translate the type of the serialized object, just like serialize()
does.
/** * d: { * name: string; * birth: Date; * } **/
Parse
parse()
to deserialize JSON string created by stringify()
.
parsejsonString /** * { * String: 'John', * Number: 28, * Set: Set([0, 1, 2, 3]), * Date: Date(1600392736750), * } **/