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

pluggable-json

v0.2.1

Published

PluggableJSON lets you serialize (to JSON) and deserialize objects and arrays with values that are otherwise not serializable to JSON.

Downloads

8

Readme

Pluggable JSON

Build Status

PluggableJSON lets you serialize (to JSON) and deserialize objects and arrays with values that are otherwise not serializable to JSON. This means you can now serialize values like Infinity, NaN, and your own objects to send over HTTP or websockets. PluggableJSON works by accepting pluggable serializers to handle these special values.

Getting started

$ npm install pluggable-json

Sample usage

/*eslint-disable no-console */
import PluggableJSON from "pluggable-json";

// A custom class that is not JSONable
class Duration {
    constructor(number, unit) {
        this.number = number;
        this.unit = unit;
    }
    getNumber() {
        return this.number;
    }
    getUnit() {
        return this.unit;
    }
    toString() {
        return `The duration is ${this.number}${this.unit}`;
    }
}

// The serializer that can handle serializing and deserializing
// the Duration class above.
const durationSerializer = {
    type: "duration",
    isSerializable(value) {
        return value instanceof Duration;
    },
    serialize(value) {
        return `${value.getNumber()}${value.getUnit()}`;
    },
    deserialize(value) {
        let match = value.match(/^(\d+)(\w)$/);
        let number = parseInt(match[1], 10);
        let unit = match[2];
        return new Duration(number, unit);
    }
};

// The JavaScript Infinity value is not JSONable.
// This serializer handles it.
const infinitySerializer = {
    type: "infinity",
    isSerializable(value) {
        return value === Infinity;
    },
    serialize(value) {
        return "Infinity";
    },
    deserialize(value) {
        return Infinity;
    }
};

// A JavaScript object we want to send over http. It contains both values
// that JSON can handle as well as values it can't. We will use our serializers
// to handle the latter.
let payload = {
    aDuration: new Duration(10, "m"),
    name: "pluggable json",
    anInfinityValue: Infinity
};

// Instantiate PluggableJSON with our serializers
let pluggableJSON = new PluggableJSON([durationSerializer, infinitySerializer]);

// serialize
let serializedPayload = pluggableJSON.serialize(payload);

// the string that can be sent across http or websockets
console.log(`Serialized payload: ${serializedPayload}`);
// Deserialized payload: Serialized payload: {"aDuration":"$duration$10m","name":"pluggable json","anInfinityValue":"$infinity$Infinity"}

// Deserialize back into the real values
let deserializedPayload = pluggableJSON.deserialize(serializedPayload);

console.log(`aDuration: ${deserializedPayload.aDuration.toString()}`);
// aDuration: The duration is 10m

console.log(`Deserialized Infinity is equal to Infinity: ${deserializedPayload.anInfinityValue === Infinity}`);
// Deserialized Infinity is equal to Infinity: true

API

new PluggableJSON(serializers [, separator])

Instantiate a new PluggableJSON object with an array of serializers and an optional separator. The separator defaults to : and is used in encoding object properties and array values. If : is a commonly occurring string in your field names, specify a different separator.

serialize(value [, options])

Serializes the given object or array to JSON, using the passed in serializers where applicable.

Options:

  • toObject (defaults to false): If true is passed, returns an object that can be safely passed to JSON.stringify.

deserialize(value)

Deserializes the given JSON (or parsed object/array) and returns the original object or array with everything "hydrated."

Custom Serializers

Each custom serializer is an object that must implement the following properties.

type

This property specifies the value type that this serializer serializes. The type is used in serializing and deserializing to keep track of which value is which type. The type must be unique across all serializers passed into the constructor.

isSerializable(value)

A function that accepts a value and returns whether this serializer can serialize it. This check should be as strict as possible so the serializer doesn't end up serializing another serializer's value. For example, if serializing an instance of a class, you can use instanceof.

serialize(value)

Given a value, return the serialized string value.

deserialize(value)

Given a serialized string, return the deserialized object.