All Projects → blitz-js → Superjson

blitz-js / Superjson

Licence: mit
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Superjson

Jackson Core
Core part of Jackson that defines Streaming API as well as basic shared abstractions
Stars: ✭ 2,003 (+349.1%)
Mutual labels:  hacktoberfest, json
Mtgjson
MTGJSON build scripts for Magic: the Gathering
Stars: ✭ 191 (-57.17%)
Mutual labels:  hacktoberfest, json
Json Api
Implementation of JSON API in PHP 7
Stars: ✭ 171 (-61.66%)
Mutual labels:  hacktoberfest, json
Smoke
💨 Simple yet powerful file-based mock server with recording abilities
Stars: ✭ 142 (-68.16%)
Mutual labels:  hacktoberfest, json
Lsp Mode
Emacs client/library for the Language Server Protocol
Stars: ✭ 3,691 (+727.58%)
Mutual labels:  hacktoberfest, json
Configurate
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation
Stars: ✭ 148 (-66.82%)
Mutual labels:  hacktoberfest, json
Circe
Yet another JSON library for Scala
Stars: ✭ 2,223 (+398.43%)
Mutual labels:  hacktoberfest, json
Json Decoder
JsonDecoder implementation that allows you to convert your JSON data into PHP class objects
Stars: ✭ 109 (-75.56%)
Mutual labels:  hacktoberfest, json
Jackson Databind
General data-binding package for Jackson (2.x): works on streaming API (core) implementation(s)
Stars: ✭ 2,959 (+563.45%)
Mutual labels:  hacktoberfest, json
Horaires Ratp Api
Webservice pour les horaires et trafic RATP en temps réel
Stars: ✭ 232 (-47.98%)
Mutual labels:  hacktoberfest, json
Packages
📦 Package configurations - The #1 free and open source CDN built to make life easier for developers.
Stars: ✭ 139 (-68.83%)
Mutual labels:  hacktoberfest, json
Hyperjson
A hyper-fast Python module for reading/writing JSON data using Rust's serde-json.
Stars: ✭ 374 (-16.14%)
Mutual labels:  hacktoberfest, json
Fossurl
Your Own Url Shortner Without any fancy server side processing and support for custom url , which can even be hosted on GitHub Pages
Stars: ✭ 131 (-70.63%)
Mutual labels:  hacktoberfest, json
Browser Extension Json Discovery
Browser (Chrome, Firefox) extension for JSON discovery
Stars: ✭ 157 (-64.8%)
Mutual labels:  hacktoberfest, json
Config Lint
Command line tool to validate configuration files
Stars: ✭ 118 (-73.54%)
Mutual labels:  hacktoberfest, json
Json C
https://github.com/json-c/json-c is the official code repository for json-c. See the wiki for release tarballs for download. API docs at http://json-c.github.io/json-c/
Stars: ✭ 2,313 (+418.61%)
Mutual labels:  hacktoberfest, json
Circe Yaml
YAML parser for circe using SnakeYAML
Stars: ✭ 102 (-77.13%)
Mutual labels:  hacktoberfest, json
Json Against Humanity
Finally, Cards Against Humanity as plain text and JSON.
Stars: ✭ 105 (-76.46%)
Mutual labels:  hacktoberfest, json
Json 2 Csv
Convert JSON to CSV *or* CSV to JSON!
Stars: ✭ 210 (-52.91%)
Mutual labels:  hacktoberfest, json
5e Database
Database for the D&D 5th Edition API
Stars: ✭ 354 (-20.63%)
Mutual labels:  hacktoberfest, json

superjson

Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.

All Contributors npm Language grade: JavaScript CI

Key features

  • 🍱 Reliable serialization and deserialization
  • 🔐 Type safety with autocompletion
  • 🐾 Negligible runtime footprint
  • 💫 Framework agnostic
  • 🛠 Perfect fix for Next.js's serialisation limitations in getServerSideProps and getInitialProps

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

Basic 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) });

// jsonString === '{"json":{"date":"1970-01-01T00:00:00.000Z"},"meta":{"values":{date:"Date"}}}'

And parse your JSON like so:

const object = superjson.parse<{ date: Date }>(jsonString);

// object === { date: new Date(0) }

Advanced Usage

For cases where you want lower level access to the json and meta data in the output, you can use the serialize and deserialize functions.

One great use case for this is where you have an API that you want to be JSON compatible for all clients, but you still also want to transmit the meta data so clients can use superjson to fully deserialize it.

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'],
};
*/

Using with Next.js

The getServerSideProps, getInitialProps, and getStaticProps data hooks provided by Next.js do not allow you to transmit Javascript objects like Dates. It will error unless you convert Dates to strings, etc.

Thankfully, Superjson is a perfect tool to bypass that limitation!

Install the library with your package manager of choice, e.g.:

yarn add -D babel-plugin-superjson-next

Add the plugin to your .babelrc. If you don't have one, create it.

{
  "presets": ["next/babel"],
  "plugins": [
    ...
    "superjson-next" // 👈
  ]
}

Done! Now you can safely use all JS datatypes in your getServerSideProps / etc. .

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
Error

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Dylan Brookes

💻 📖 🎨 ⚠️

Simon Knott

💻 🤔 ⚠️ 📖

Brandon Bayer

🤔

Jeremy Liberman

⚠️ 💻

Joris

💻

tomhooijenga

💻

Ademílson F. Tonato

⚠️

Piotr Monwid-Olechnowicz

🤔

Alex Johansson

💻 ⚠️

Simon Edelmann

🐛 💻 🤔

Sam Garson

🐛

Mark Hughes

🐛

Lxxyx

💻

Máximo Mussini

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Prior art

Other libraries that aim to solve a similar problem:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].