1. destr
A faster, secure and convenient alternative for JSON.parse
destr
Package: destr
Created by: unjs
Last modified: Tue, 20 Feb 2024 09:44:42 GMT
Version: 2.0.3
License: MIT
Downloads: 7,238,926
Repository: https://github.com/unjs/destr

Install

npm install destr
yarn add destr

destr

npm version
npm downloads
bundle
License

A faster, secure and convenient alternative for JSON.parse.

Usage

Node.js

Install dependency:

 # npm
npm i destr

# yarn
yarn add destr

# pnpm
pnpm i destr

Import into your Node.js project:

 // ESM
import { destr, safeDestr } from "destr";

// CommonJS
const { destr, safeDestr } = require("destr");

Deno

 import { destr, safeDestr } from "https://deno.land/x/destr/src/index.ts";

console.log(destr('{ "deno": "yay" }'));

Why?

✅ Type Safe

 const obj = JSON.parse("{}"); // obj type is any

const obj = destr("{}"); // obj type is unknown by default

const obj = destr<MyInterface>("{}"); // obj is well-typed

✅ Fast fallback to input if is not string

 // Uncaught SyntaxError: Unexpected token u in JSON at position 0
JSON.parse();

// undefined
destr();

✅ Fast lookup for known string values

 // Uncaught SyntaxError: Unexpected token T in JSON at position 0
JSON.parse("TRUE");

// true
destr("TRUE");

✅ Fallback to original value if parse fails (empty or any plain string)

 // Uncaught SyntaxError: Unexpected token s in JSON at position 0
JSON.parse("salam");

// "salam"
destr("salam");

Note: This fails in safe/strict mode with safeDestr.

✅ Avoid prototype pollution

 const input = '{ "user": { "__proto__": { "isAdmin": true } } }';

// { user: { __proto__: { isAdmin: true } } }
JSON.parse(input);

// { user: {} }
destr(input);

✅ Strict Mode

When using safeDestr it will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)

 // Returns "[foo"
destr("[foo");

// Throws an error
safeDestr("[foo");

Benchmarks

destr is faster generally for arbitrary inputs but also sometimes little bit slower than JSON.parse when parsing a valid JSON string mainly because of transform to avoid prototype pollution which can lead to serious security issues if not being sanitized. In the other words, destr is better when input is not always a JSON string or from untrusted source like request body.

Check Benchmark Results or run with pnpm run bench:node or pnpm run bench:bun yourself!

License

MIT. Made with 💖

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin