Skip to content

Stringify a Javascript JSON object, and actually handle Maps and Sets

License

Notifications You must be signed in to change notification settings

stefnotch/json-safe-stringify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-safe-stringify

NPM Package

Fixes the issues with https://stackoverflow.com/a/56150320/3492994 and other answers on that page.

Example

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

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

{
    "compilerOptions": {
        /* This tells Typescript that we're working with the new importing mechanisms */
        "module": "nodenext", 
    }
}

Make sure to read the documentation for what that means, among other things all the imports in your code might need a file extension.

About

Stringify a Javascript JSON object, and actually handle Maps and Sets

Resources

License

Stars

Watchers

Forks

Packages

No packages published