the-serializer
v3.0.1
Published
a certain specific JSON stringifier that supports `BigInt`, `Symbol` and ..., and can possibly stringify your classes and revive them after parsing.
Downloads
7
Maintainers
Readme
The serializer
a certain specific JSON stringifier that supports:
- BigInt
- Symbol
- undefined
- Infinity
- Date
- Map
- Set
- RegExp
- URL
and can possibly stringify your classes and revive them after parsing.
Usage
pnpm install the-serializer
import {serialize, deserialize} from "the-serializer";
deserialize(serialize(BigInt(10))) === BigInt(10)
// more advanced
// to use class serialization and revival you need to implement `toJSON` and `fromJSON`
// in your class, and pass your class to serialize and deserialize
class User {
constructor(
public name: string,
public age: number,
) {}
toJSON() {
return serialize([this.name, this.age]);
}
static fromJSON(json: string) {
const [name, age] = deserialize(json);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return new User(name, age);
}
}
const str = serialize(new User('john', 25), {User});
console.log(str); // '["{#$_C:User}[[\"1\",25],\"john\"]"]'
const deserializedUser = deserialize(str, {User});
console.log(deserializedUser instanceof User); // true
Credits
uses flatted under the hood to handle circular references.
some ideas from serialize-javascript
.