npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-strictify

v8.0.3

Published

Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.

Downloads

387

Readme

Typescript npm GitHub Workflow Status GitHub Issues libraries.io Codecov npm bundle size

json-strictify

Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.

Why

The native JSON.stringify function drops or silently modifies all values that are not supported by the JSON specification:

JSON.stringify({ a: 42, b: undefined })
// returns '{"a":42}'

JSON.parse(JSON.stringify(NaN))
// returns null

JSON.stringify([1, NaN, 3])
// returns '[1,null,3]'

In many cases this is not the behaviour you want: relying on the serialization method to clean up your data is error prone and can lead to subtle bugs that are annoying to find. json-strictify helps you to easily avoid these issues with literally a single line of code.

Unlike json-stringify-safe it does not attempt to "fix" its input but always bails out when it encounters something that would prevent it from being serialized properly.


Installation

Simply install via npm:

npm install json-strictify

Usage

json-strictify exposes three methods: stringify, parse and enable, so it can be used as a drop-in replacement for the native JSON object:

import JSON from 'json-strictify'

JSON.stringify(someObject)

The parse method is simply a reference to the native JSON.parse function.


Examples

The stringify function throws an error if the input to be serialized contains invalid values:

import JSONs from 'json-strictify'

JSONs.stringify({ x: 42, y: NaN })
// InvalidValueError: Invalid value at /y (NaN is not JSON-serializable)

Also, if the data you want to stringify contains circular references a CircularReferenceError is thrown:

const data = []
data.push(data)
JSONs.stringify(data)
// CircularReferenceError: Circular reference found at "/0"

The location of the value that caused the error is given as a JSON Pointer reference.


ESLint integration

If you want to ensure that all serialization is done through json-strictify you can disable the global JSON variable like so:

"globals": {
    "JSON": "off"
}

See the ESLint documentation on configuring globals for details.


Disabling json-strictify

In production you may not want to have the additional overhead introduced by json-strictify. This can easily be avoided by calling the enabled method:

import JSONs from 'json-strictify'
const JSON = JSONs.enabled(config.debug)

// or for older versions of Javascript:
const JSON = require('json-strictify').enabled(config.debug)

If called with a falsy parameter, enabled will return an object that delegates directly to the native JSON object so there will be no performance penalty whatsoever.

Note: json-strictify is disabled by default if NODE_ENV is set to production (you may still enable it manually, of course).