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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@austinbreslin/safe-json

v1.0.1

Published

- [**Safe-JSON**: Handle JSON Safely in TypeScript and JavaScript](#safe-json-handle-json-safely-in-typescript-and-javascript) - [Why Use **Safe-JSON**?](#why-use-safe-json) - [Features](#features) - [Installation](#installation) - [npm](#npm)

Downloads

3

Readme

Safe-JSON: Handle JSON Safely in TypeScript and JavaScript

Why Use Safe-JSON?

JavaScript's native JSON.parse and JSON.stringify can throw runtime errors that are not reflected in TypeScript definitions, making them error-prone and unpredictable. These errors often stem from JavaScript quirks and limitations, such as:

  • Handling of null and undefined.
  • Special numeric values (NaN, Infinity).
  • Invalid Date objects.
  • BigInt values.
  • Circular references in objects.

Safe-JSON eliminates this confusion by offering safe alternatives that never throw. Instead, all methods return a tuple of [error, result], allowing you to handle errors explicitly and gracefully.


Features

Safe-JSON handles:

  • Null/Undefined: Normalizes null and undefined to JSON-compliant null.
  • NaN and Infinity: Converts invalid numeric values to null.
  • Invalid Dates: Represents invalid dates as null.
  • BigInts: Properly serializes and deserializes BigInt values.
  • Circular References: Detects and replaces circular references with descriptive pointers (e.g., $ref.root.path).
  • Custom Serialization: Enables error-free handling of complex or non-standard JavaScript objects.

Installation

npm

npm install @austinbreslin/safe-json

pnpm

pnpm add @austinbreslin/safe-json

Usage


import { stringify, parse } from "@austinbreslin/safe-json";

const test = {
  bigInt: 1234567890123456789012345678901234567890n,
  circular: { inner: null },
  invalidDate: new Date('invalid-date'),
  nan: NaN,
  undefined: undefined,
  null: null,
  nested: {
    array: [{ test: 'test' }],
    object: {
      innerObject: { innerArray: [{ test: 'test' }] }
    }
  }
};

// Introduce circular reference
test.circular.inner = test.circular;

// Safely stringify
const [strErr, stringified] = stringify(test);
if (strErr) {
  console.error("Stringify error:", strErr);
} else {
  console.log("Stringified JSON:", stringified);
}

/*
{
  "bigInt": "1234567890123456789012345678901234567890",
  "circular": {
    "inner": "$ref.root.circular"
  },
  "invalidDate": null,
  "nan": null,
  "undefined": null,
  "nested": {
    "array": [{ "test": "test" }],
    "object": {
      "innerObject": {
        "innerArray": [{ "test": "test" }]
      }
    }
  }
}
*/

// Safely parse
const [parErr, parsed] = parse(stringified);
if (parErr) {
  console.error("Parse error:", parErr);
} else {
  console.log("Parsed object:", parsed);
}

Notes

Performance

While Safe-JSON is optimized for safety and flexibility, it cannot match the raw speed of native JSON methods due to additional processing. However, for very large JSON objects, it may perform better in real-world scenarios because it leverages generators to prevent blocking the event loop.


If performance is a critical concern, consider using native JSON methods where safety is less of a priority or exploring alternatives like BSON for more efficient serialization.

Package Size

The package size is around 6kb, npm lists the type definitions and both the Common JS and ESM build. You will only use one of these.

Design Goals

The primary goal of Safe-JSON is to eliminate undefined behavior and prevent unexpected runtime errors. It aims to:


Provide clear and predictable error handling. Simplify debugging and reduce crashes. Standardize JSON serialization and deserialization across edge cases.

Detailed Features

Normalizing Null/Undefined/NaN/Invalid Dates

JavaScript allows multiple representations of null (e.g., undefined, NaN, invalid Date objects). JSON only supports null. Safe-JSON converts these variations to JSON-compliant null.

BigInts

Native JSON does not support JavaScript's BigInt type. Safe-JSON properly serializes BigInt values and restores them during deserialization.

Circular References

Circular references cause native JSON.stringify to throw an error. Safe-JSON detects and replaces circular references with descriptive pointers (e.g., $ref.root.path). This ensures error-free serialization while preserving structure.

Infinity

JavaScript's Infinity constants represent invalid numbers in JSON. Safe-JSON replaces these values with null during serialization to maintain JSON compliance.

Summary

Safe-JSON is designed to:

Improve safety and predictability in JSON handling. Handle JavaScript quirks seamlessly. Provide a reliable solution for complex data structures. If your application requires robust error handling and compatibility with non-standard JSON scenarios, Safe-JSON is the perfect tool for the job.