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-with-bigint

v2.1.2

Published

JS library that allows you to easily serialize and deserialize data with BigInt values

Downloads

2,177

Readme

JSON with BigInt

JS library that allows you to easily serialize and deserialize data with BigInt values

Why would I need JSON-with-BigInt?

3 reasons:

  1. You need to convert some data to/from JSON and it includes BigInt values
  2. Native JSON.stringify() and JSON.parse() methods in JS can't work with BigInt
  3. Other libraries and pieces of code that you'll find either can't solve this problem while supporting consistent round-trip operations (meaning, you will not get the same BigInt values if you serialize and then deserialize them) or requires you to change your JSON or the way you want to work with your data

Good things about JSON-with-BigInt

✔️ Supports consistent round-trip operations with JSON

const data = { bigNumber: 9007199254740992n };
JSONStringify(data) // '{"bigNumber":9007199254740992}'
JSONParse(JSONStringify(data)).bigNumber === 9007199254740992n // true

✔️ No need to change your JSON or the way you want to work with your data

✔️ Parses and stringifies all other values other than big numbers same way as native JSON methods in JS do

✔️ Correctly parses float numbers and negative numbers

✔️ Does not contaminate your global space (unlike monkey-patching solution)

✔️ You don't have to memorize this library's API, you already know it, just skip the dot, use camelCase and that's it (JSONParse(), JSONStringify())

✔️ Can be used in a TypeScript project (.d.ts file included)

✔️ Size: 424 bytes (minified and gzipped)

✔️ No dependencies

Getting Started

This library has no default export. Why it's a good thing

NPM

Add this library to your project using NPM

npm i json-with-bigint

and use it

import { JSONParse, JSONStringify } from 'json-with-bigint';

const userData = {
  someBigNumber: 9007199254740992n
};

localStorage.setItem('userData', JSONStringify(userData));

const restoredUserData = JSONParse(localStorage.getItem('userData') || '');

CDN

Add this code to your HTML

<script src="https://cdn.jsdelivr.net/npm/json-with-bigint/json-with-bigint.min.js"></script>

and use it

<script>
  const userData = {
    someBigNumber: 9007199254740992n
  };

  localStorage.setItem('userData', JSONStringify(userData));

  const restoredUserData = JSONParse(localStorage.getItem('userData') || '');
</script>

Manually

Download json-with-bigint.min.js from this repository to your project's folder and use it

<script src="./json-with-bigint.min.js"></script>
<script>
  const userData = {
    someBigNumber: 9007199254740992n
  };

  localStorage.setItem('userData', JSONStringify(userData));

  const restoredUserData = JSONParse(localStorage.getItem('userData') || '');
</script>

How to use

JSONParse - works just like native JSON.parse, but supports BigInt

JSONStringify - works just like native JSON.stringify, but supports BigInt

Examples:

  • JSONParse('{"someBigNumber":9007199254740992n}')
  • JSONStringify({ someBigNumber: 9007199254740992n })