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

use-json

v1.0.1

Published

A TypeScript library that extends JavaScript's native JSON functionality to support serialization of Maps, Sets, and RegExp objects.

Readme

use-json

A TypeScript library that extends JavaScript's native JSON functionality to support serialization of Maps, Sets, and RegExp objects.

TypeScript Vitest MIT License

Why This Package?

JavaScript's built-in JSON.stringify() and JSON.parse() functions don't support serializing certain built-in objects like Map, Set, and RegExp. When you try to serialize these objects:

// Standard JSON
JSON.stringify(new Map([['key', 'value']])); // '{}'
JSON.stringify(new Set([1, 2, 3])); // '{}'
JSON.stringify(/test/gi); // '{}'

This library solves this limitation by providing an enhanced JSON serializer that properly handles these data types while maintaining compatibility with the standard JSON format.

🚀 Features

  • ✅ Full support for Map, Set, and RegExp serialization
  • 🔄 Maintains the original JSON API's simplicity
  • 📦 Type-safe with TypeScript
  • 🎯 Compatible with custom replacer and reviver functions
  • 🔍 Preserves all object properties (like RegExp flags)
  • 🎭 Handles nested structures seamlessly

📦 Installation

npm install use-json
# or
yarn add use-json

🛠️ Usage

Basic Usage

import JSON from 'use-json';

// Maps
const map = new Map([['key', 'value']]);
const mapJson = JSON.stringify(map);
const mapBack = JSON.parse(mapJson);
// mapBack instanceof Map === true

// Sets
const set = new Set([1, 2, 3]);
const setJson = JSON.stringify(set);
const setBack = JSON.parse(setJson);
// setBack instanceof Set === true

// RegExp
const regexp = /test/gi;
const regexpJson = JSON.stringify(regexp);
const regexpBack = JSON.parse(regexpJson);
// regexpBack instanceof RegExp === true

Nested Structures

const complex = {
	map: new Map([['key', new Set([1, 2, 3])]]),
	set: new Set([new Map([['nested', 'value']])]),
	regexp: /test/gi
};

const json = JSON.stringify(complex);
const restored = JSON.parse(json);
// All nested structures are properly restored

Custom Replacer and Reviver

// Custom replacer
const json = JSON.stringify(data, (key, value) => {
	if (key === 'sensitive') return undefined;
	return value;
});

// Custom reviver
const obj = JSON.parse(json, (key, value) => {
	if (key === 'date') return new Date(value);
	return value;
});

🧪 Testing

# Run tests
yarn test

📝 How It Works

The library works by:

  1. Adding type identifiers to serialized objects (__map__, __set__, __regexp__)
  2. Converting Maps and Sets to arrays during serialization
  3. Preserving RegExp flags and source
  4. Automatically reconstructing the original objects during parsing

📄 License

MIT

👨‍💻 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

👨‍💻 Author

Felipe Rohde