https://github.com/stefnotch/json-safe-stringify
Stringify a Javascript JSON object, and actually handle Maps and Sets
https://github.com/stefnotch/json-safe-stringify
Last synced: about 1 year ago
JSON representation
Stringify a Javascript JSON object, and actually handle Maps and Sets
- Host: GitHub
- URL: https://github.com/stefnotch/json-safe-stringify
- Owner: stefnotch
- License: bsd-3-clause
- Created: 2023-01-16T08:39:03.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-15T21:02:55.000Z (over 2 years ago)
- Last Synced: 2025-03-29T00:05:57.243Z (about 1 year ago)
- Language: TypeScript
- Size: 273 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-safe-stringify
[NPM Package](https://www.npmjs.com/package/json-safe-stringify)
Fixes the issues with https://stackoverflow.com/a/56150320/3492994 and other answers on that page.
Example
```ts
const serializer = new JsonSerializer();
const text = serializer.stringify({ map: new Map([["a", 1]]) });
console.log(text);
const obj = serializer.parse(text, (error) => console.error(error));
```
Or use it directly
```ts
const text = JSON.stringify(
{ map: new Map([["a", 1]]) },
jsonStringifyReplacer
);
console.log(text);
const obj = JSON.parse(text, (key, value) =>
jsonParseReviver(key, value, (error) => console.error(error))
);
```
## FAQ
- Typescript cannot find the library?
That's because this uses the [`"export"` field in the `package.json`](https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing). Go to your `tsconfig.json`, or create one if there is none, and set
```json
{
"compilerOptions": {
/* This tells Typescript that we're working with the new importing mechanisms */
"module": "nodenext",
}
}
```
Make sure to read [the documentation](https://www.typescriptlang.org/docs/handbook/esm-node.html) for what that means, among other things all the imports in your code might need a file extension.