Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
fork-the-json-for-temp
Advanced tools
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more.
getServerSideProps
and getInitialProps
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
.
Install the library with your package manager of choice, e.g.:
yarn add superjson
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) }
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'],
};
*/
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. .
Serializes any JavaScript value into a JSON-compatible object.
const object = {
normal: 'string',
timestamp: new Date(),
test: /superjson/,
};
const { json, meta } = serialize(object);
Returns json
and meta
, both JSON-compatible values.
Deserializes the output of Superjson back into your original value.
const { json, meta } = serialize(object);
deserialize({ json, meta });
Returns your original value
.
Serializes and then stringifies your JavaScript value.
const object = {
normal: 'string',
timestamp: new Date(),
test: /superjson/,
};
const jsonString = stringify(object);
Returns string
.
Parses and then deserializes the JSON string returned by stringify
.
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 | ❌ | ✅ |
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 🐛 |
This project follows the all-contributors specification. Contributions of any kind welcome!
Other libraries that aim to solve a similar problem:
FAQs
Unknown package
We found that fork-the-json-for-temp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.