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 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-stringify-raw

v0.1.0

Published

Variant of JSON.stringify where the value returned by the replacer function is used verbatim.

Readme

json-stringify-raw

Build Status: Linux Build Status: Windows Coverage Dependency Status Supported Node Version Version on NPM

An implementation of JSON.stringify where strings returned by the replacer function are used verbatim. This gives callers more control over how values are stringified, which can be used to address interoperability issues, such as:

  • Parsers which distinguish integers from decimal/floating point numbers (e.g. 1 is treated differently from 1.0).
  • Parsers which assign significance to the number of fractional digits (e.g. treating 1.00 as less precise than 1.000).
  • Parsers which distinguish exponential from non-exponential representations.
  • Parsers which require special string escaping (e.g. for incorrect whitespace or charset handling).

It can also be used to produce extensions to JSON which can represent NaN, Infinity, Date, RegExp, or other values not representable in JSON.

Introductory Example

To produce JSON where all numbers have two digits after the decimal:

const stringify = require('json-stringify-raw');
function replaceFixed(key, value) {
  if (typeof value === 'number') {
    return value.toFixed(2);
  }
}
stringify([1, 2, true], replaceFixed); // '[1.00,2.00,true]'

Usage

The function exported by json-stringify-raw behaves like JSON.stringify specified in ES2019 with the exception that the replacer argument must return either a string, which represents the value being replaced in the output, a boolean, to include or exclude the property from the output, or undefined/null, to indicate that the value should be stringified normally, without replacement. Any other value causes TypeError to be thrown.

Installation

This package can be installed using npm, either globally or locally, by running:

npm install json-stringify-raw

Recipes

Produce Infinity and NaN

Some JSON parsers recognize NaN and Infinity, which are not permitted in JSON (as defined by RFC 8259). To produce these:

const stringify = require('json-stringify-raw');
function replaceInfNaN(key, value) {
  switch (value) {
    case Infinity: return 'Infinity';
    case -Infinity: return '-Infinity';
    default: if (Number.isNaN(value)) return 'NaN';
  }
}
stringify([NaN, null, Infinity], replaceInfNaN); // '[NaN,null,Infinity]'

Omit values in Arrays

Unlike array initializers in JavaScript, JSON requires every array element to be specified. To produce JSON which omits missing and undefined values from arrays:

const stringify = require('json-stringify-raw');
function replaceArrayUndef(key, value) {
  if (value === undefined && Array.isArray(this)) {
    return '';
  }
}
stringify([0, , undefined, null], arrayUndef); // '[0,,,null]'

Warning: The above code represents [1,undefined] as [1,] which is a JavaScript array initializer for an array with length 1, rather than the input value which has length 2 (and could be represented as [1,,]). Consider how a JSON parser would interpret JSON with trailing comma.

More examples can be found in the test specifications.

API Docs

To use this module as a library, see the API Documentation.

Contributing

Contributions are appreciated. Contributors agree to abide by the Contributor Covenant Code of Conduct. If this is your first time contributing to a Free and Open Source Software project, consider reading How to Contribute to Open Source in the Open Source Guides.

If the desired change is large, complex, backwards-incompatible, can have significantly differing implementations, or may not be in scope for this project, opening an issue before writing the code can avoid frustration and save a lot of time and effort.

License

This project is available under the terms of the MIT License. See the summary at TLDRLegal.