Pluggable JSON
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 */; // A custom class that is not JSONable { thisnumber = number; thisunit = unit; } { return thisnumber; } { return thisunit; } { return `The duration is `; } // The serializer that can handle serializing and deserializing// the Duration class above.const durationSerializer = type: "duration" { return value instanceof Duration; } { return ``; } { let match = value; let number = ; let unit = match2; return number unit; }; // The JavaScript Infinity value is not JSONable.// This serializer handles it.const infinitySerializer = type: "infinity" { return value === Infinity; } { return "Infinity"; } { 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: 10 "m" name: "pluggable json" anInfinityValue: Infinity; // Instantiate PluggableJSON with our serializerslet pluggableJSON = durationSerializer infinitySerializer; // serializelet serializedPayload = pluggableJSON; // the string that can be sent across http or websocketsconsole;// Deserialized payload: Serialized payload: {"aDuration":"$duration$10m","name":"pluggable json","anInfinityValue":"$infinity$Infinity"} // Deserialize back into the real valueslet deserializedPayload = pluggableJSON; console;// aDuration: The duration is 10m console;// 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 tofalse
): Iftrue
is passed, returns an object that can be safely passed toJSON.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.