Package Exports
- superjson
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (superjson) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
[](#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 superjsonUsage
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);
/*
json = {
normal: 'string',
timestamp: "2020-06-20T04:56:50.293Z",
test: "/blitz/",
};
// note that `normal` is not included here; `meta` only has special cases
meta = {
timestamp: 'date',
test: 'regexp',
};
*/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):
Dylan Brookes 💻 📖 🎨 ⚠️ |
Simon Knott 💻 🤔 ⚠️ 📖 |
Brandon Bayer 🤔 |
Jeremy Liberman ⚠️ 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
Prior art
Other libraries that aim to solve a similar problem:
- Serialize JavaScript by Eric Ferraiuolo
- devalue by Rich Harris