the-serializer
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

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.

Package Sidebar

Install

npm i the-serializer

Weekly Downloads

0

Version

3.0.1

License

MIT

Unpacked Size

13.9 kB

Total Files

6

Last publish

Collaborators

  • jdmnjd