https://github.com/jd1378/the-serializer
a certain specific JSON stringifier
https://github.com/jd1378/the-serializer
bigint javascript serialize serializer serializers symbol typescript
Last synced: 10 days ago
JSON representation
a certain specific JSON stringifier
- Host: GitHub
- URL: https://github.com/jd1378/the-serializer
- Owner: jd1378
- License: mit
- Created: 2024-08-30T17:31:56.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-01T06:11:12.000Z (9 months ago)
- Last Synced: 2025-04-23T05:20:00.491Z (28 days ago)
- Topics: bigint, javascript, serialize, serializer, serializers, symbol, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/the-serializer
- Size: 84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# The serializer
a certain specific JSON stringifier that supports:
- BigInt
- Symbol
- undefined
- Infinity
- Date
- Map
- Set
- RegExp
- URLand can possibly stringify your classes and revive them after parsing.
## Usage
```bash
pnpm install the-serializer
``````ts
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 deserializeclass 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](https://www.npmjs.com/package/flatted) under the hood to handle circular references.
some ideas from [serialize-javascript](https://www.npmjs.com/package/serialize-javascript).