What is superjson?
Superjson is a flexible and powerful library for serializing JavaScript data structures, including those that are not supported by JSON, such as Dates, RegExps, Maps, Sets, and more. It allows for seamless serialization and deserialization of complex data types, making it easier to work with complex data in applications that require data persistence or transmission.
What are superjson's main functionalities?
Serialization of complex types
Superjson can serialize complex JavaScript types like Dates, Maps, and Sets into a string representation that can be easily transmitted or stored. Upon deserialization, these strings are converted back into their original complex types.
{"date": "2023-04-14T12:00:00.000Z", "map": "[[\"key1\",\"value1\"],[\"key2\",\"value2\"]]", "set": "[\"a\",\"b\",\"c\"]"}
Custom serialization
Developers can define custom serialization and deserialization logic for unsupported types or to override the default behavior for supported types. This feature enhances flexibility and control over how data is serialized.
{"customType": "SerializedValue"}
Other packages similar to superjson
flatted
Flatted is a package that offers serialization and deserialization of complex JavaScript objects, including nested objects and circular references, using a flat structure. Compared to Superjson, Flatted focuses on solving circular reference issues but does not explicitly handle a wide range of JavaScript types like Superjson does.
serialize-javascript
Serialize-javascript provides serialization of JavaScript objects into a string, including regular expressions and functions. It is similar to Superjson in its ability to handle non-standard JSON data types, but it is more focused on including functions in the serialization process, which Superjson does not inherently support.
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors-)
Key features
- 🍱 Reliable serialization and deserialization
- 🔐 Type safety with autocompletion
- 🐾 Negligible runtime footprint
- 💫 Framework agnostic
Backstory
At Blitz, we have struggled with the limitations of JSON. We often find ourselves working with Date
, Map
, Set
or BigInt
, but JSON.stringify
doesn't support any of them without going through the hassle of converting manually!
Superjson solves these issues by providing a thin wrapper over JSON.stringify
and JSON.parse
.
Getting started
Install the library with your package manager of choice, e.g.:
yarn add superjson
Usage
The easiest way to use Superjson is with its stringify
and parse
functions. If you know how to use JSON.stringify
, you already know Superjson!
Easily stringify any expression you’d like:
import superjson from 'superjson';
const jsonString = superjson.stringify({date: new Date(0)});
And parse your JSON like so:
const object = superjson.parse(jsonString);
Alternatively, transform any JavaScript value into a JSON-compatible one by using our lower-level serialize
and deserialize
functions.
For example:
const object = {
normal: 'string',
timestamp: new Date(),
test: /superjson/,
};
const {json, meta} = superjson.serialize(object);
API
serialize
Serializes any JavaScript value into a JSON-compatible object.
Examples
const object = {
normal: 'string',
timestamp: new Date(),
test: /superjson/,
};
const {json, meta} = serialize(object);
Returns json
and meta
, both JSON-compatible values.
deserialize
Deserializes the output of Superjson back into your original value.
Examples
const {json, meta} = serialize(object);
deserialize({json, meta});
Returns your original value
.
stringify
Serializes and then stringifies your JavaScript value.
Examples
const object = {
normal: 'string',
timestamp: new Date(),
test: /superjson/,
};
const jsonString = stringify(object);
Returns string
.
parse
Parses and then deserializes the JSON string returned by stringify
.
Examples
const jsonString = stringify(object);
parse(jsonString);
Returns string
.
Superjson supports many extra types which JSON does not. You can serialize all these:
type | supported by standard JSON? | supported by Superjson? |
---|
string | ✅ | ✅ |
number | ✅ | ✅ |
boolean | ✅ | ✅ |
null | ✅ | ✅ |
Array | ✅ | ✅ |
Object | ✅ | ✅ |
undefined | ❌ | ✅ |
bigint | ❌ | ✅ |
Date | ❌ | ✅ |
RegExp | ❌ | ✅ |
Set | ❌ | ✅ |
Map | ❌ | ✅ |
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Prior art
Other libraries that aim to solve a similar problem: