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

refjson

v0.0.2

Published

RefJSON is a TypeScript library for serializing and deserializing JavaScript objects, with support for nested and circular references. It extends the capabilities of `JSON.stringify` and `JSON.parse` by preserving object references, allowing for accurate

Downloads

158

Readme

RefJSON

RefJSON is a TypeScript library for serializing and deserializing JavaScript objects, with support for nested and circular references. It extends the capabilities of JSON.stringify and JSON.parse by preserving object references, allowing for accurate reconstruction of complex object graphs.

Features

  • Preserves Object References: Handles nested and circular references within objects, ensuring that the serialized data accurately represents the original object structure.
  • External References: Supports serialization and deserialization of objects that reference external resources not included in the serialization.
  • Primitive Types Support: Seamlessly serializes and deserializes primitive types, behaving like native JSON methods.

Installation

Install the library via npm:

npm install refjson

Usage

Importing

import { serialize, deserialize } from 'refjson';
// or
import * as refjson from 'refjson';

Serializing and Deserializing Objects

Handling Circular References

RefJSON automatically handles objects with circular references.

const alice = { name: 'Alice' };
const bob = { name: 'Bob', friend: alice };
alice.friend = bob; // Circular reference

const jsonString = refjson.serialize(alice);

const reconstructedAlice = refjson.deserialize(jsonString);

console.log(reconstructedAlice.friend.friend === reconstructedAlice); // true

External References

Serialize objects that reference external resources by providing a mapping of external objects to identifiers.

const externalResource = { name: 'External Resource' };
const data = { value: 42, resource: externalResource };

// Map external objects to identifiers
const externRefToName = new Map<any, string>();
externRefToName.set(externalResource, 'externalResourceId');

// Serialize with external references
const jsonString = refjson.serialize(data, false, externRefToName);

During deserialization, provide a mapping from identifiers back to the external objects.

// Map identifiers back to external objects
const externNameToRef = new Map<string, any>();
externNameToRef.set('externalResourceId', externalResource);

const reconstructedData = refjson.deserialize(jsonString, externNameToRef);

console.log(reconstructedData.resource === externalResource); // true

Serializing Primitive Types

RefJSON supports serialization of primitive types, similar to JSON.stringify.

console.log(refjson.serialize(123)); // "123"
console.log(refjson.deserialize(refjson.serialize(123))); // 123

console.log(refjson.serialize('Hello, World!')); // "\"Hello, World!\""
console.log(refjson.deserialize(refjson.serialize('Hello, World!'))); // "Hello, World!"

API Reference

serialize(val: unknown, formatted?: boolean, externRefToName?: Map<any, string> | WeakMap<any, string>): string

Serializes a JavaScript value, preserving object references.

  • val: The value to serialize.
  • formatted (optional): If true, the output JSON string will be formatted with indentation for readability. Defaults to false.
  • externRefToName (optional): A map of external objects to their identifiers. External objects are not included in the serialized data but are referenced by their identifiers.

deserialize(str: string, externNameToRef?: Map<string, any>): unknown

Deserializes a JSON string produced by serialize, reconstructing the original object graph.

Error Handling

  • Missing External Reference Map: If external references are present in the serialized data but externNameToRef is not provided during deserialization, an error is thrown.
const jsonString = refjson.serialize(data, false, externRefToName);

// Throws error because externNameToRef is missing
refjson.deserialize(jsonString);
  • Unresolved External References: If an external reference identifier is not found in externNameToRef, an error is thrown.
const externNameToRef = new Map<string, any>();
// Missing 'externalResourceId' mapping

// Throws error because 'externalResourceId' is not in externNameToRef
refjson.deserialize(jsonString, externNameToRef);

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.