{"name":"destr","version":"2.0.5","description":"A faster, secure and convenient alternative for JSON.parse","repository":{"type":"git","url":"git+https://github.com/unjs/destr.git"},"license":"MIT","sideEffects":false,"type":"module","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./lib/index.cjs"}},"main":"./lib/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","scripts":{"bench:bun":"pnpm build && bun --bun ./test/bench.mjs","bench:node":"pnpm build && node ./test/bench.mjs","build":"unbuild","dev":"vitest dev","lint":"eslint . && prettier -c src test","lint:fix":"eslint . --fix && prettier -w src test","release":"pnpm test && pnpm build && changelogen --release --push && npm publish","test":"pnpm lint && vitest run --coverage"},"devDependencies":{"@hapi/bourne":"^3.0.0","@vitest/coverage-v8":"^3.1.1","changelogen":"^0.6.1","eslint":"^9.23.0","eslint-config-unjs":"^0.4.2","mitata":"^1.0.34","prettier":"^3.5.3","secure-json-parse":"^4.0.0","typescript":"^5.8.2","unbuild":"~3.4","vitest":"^3.1.1"},"packageManager":"pnpm@10.7.1","_id":"destr@2.0.5","gitHead":"7bb3c39ef5f8c84219be08ebc11b3c4f4a4c828f","bugs":{"url":"https://github.com/unjs/destr/issues"},"homepage":"https://github.com/unjs/destr#readme","_nodeVersion":"22.14.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-ugFTXCtDZunbzasqBxrK93Ik/DRYsO6S/fedkWEMKqt04xZ4csmnmwGDBAb07QWNaGMAmnTIemsYZCksjATwsA==","shasum":"7d112ff1b925fb8d2079fac5bdb4a90973b51fdb","tarball":"http://registry-npm.ywwl.com/destr/download/destr-2.0.5.tgz","fileCount":9,"unpackedSize":11035,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCX5nr+4vbgOrt/v6HT4vStp4JczJMxGDLdlpaNeygYVQIhAKsqHlk4WNpX/33w1QOF5m1dyyewmCHiCZPkwil5SDPn"}],"size":3807,"noattachment":false,"key":"/destr/-/destr-2.0.5.tgz"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"directories":{},"maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/destr_2.0.5_1743698475442_0.8823194809253956"},"_hasShrinkwrap":false,"readme":"# destr\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![License][license-src]][license-href]\n\nA faster, secure and convenient alternative for [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse).\n\n## Usage\n\n### Node.js\n\nInstall dependency:\n\n```bash\n# npm\nnpm i destr\n\n# yarn\nyarn add destr\n\n# pnpm\npnpm i destr\n```\n\nImport into your Node.js project:\n\n```js\n// ESM\nimport { destr, safeDestr } from \"destr\";\n\n// CommonJS\nconst { destr, safeDestr } = require(\"destr\");\n```\n\n### Deno\n\n```js\nimport { destr, safeDestr } from \"https://deno.land/x/destr/src/index.ts\";\n\nconsole.log(destr('{ \"deno\": \"yay\" }'));\n```\n\n## Why?\n\n### ✅ Type Safe\n\n```ts\nconst obj = JSON.parse(\"{}\"); // obj type is any\n\nconst obj = destr(\"{}\"); // obj type is unknown by default\n\nconst obj = destr<MyInterface>(\"{}\"); // obj is well-typed\n```\n\n### ✅ Fast fallback to input if is not string\n\n```js\n// Uncaught SyntaxError: Unexpected token u in JSON at position 0\nJSON.parse();\n\n// undefined\ndestr();\n```\n\n### ✅ Fast lookup for known string values\n\n```js\n// Uncaught SyntaxError: Unexpected token T in JSON at position 0\nJSON.parse(\"TRUE\");\n\n// true\ndestr(\"TRUE\");\n```\n\n### ✅ Fallback to original value if parse fails (empty or any plain string)\n\n```js\n// Uncaught SyntaxError: Unexpected token s in JSON at position 0\nJSON.parse(\"salam\");\n\n// \"salam\"\ndestr(\"salam\");\n```\n\n**Note:** This fails in safe/strict mode with `safeDestr`.\n\n### ✅ Avoid prototype pollution\n\n```js\nconst input = '{ \"user\": { \"__proto__\": { \"isAdmin\": true } } }';\n\n// { user: { __proto__: { isAdmin: true } } }\nJSON.parse(input);\n\n// { user: {} }\ndestr(input);\n```\n\n### ✅ Strict Mode\n\nWhen 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)\n\n```js\n// Returns \"[foo\"\ndestr(\"[foo\");\n\n// Throws an error\nsafeDestr(\"[foo\");\n```\n\n## Benchmarks\n\n`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](https://learn.snyk.io/lessons/prototype-pollution/javascript/) 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.\n\nCheck [Benchmark Results](./BENCH.md) or run with `pnpm run bench:node` or `pnpm run bench:bun` yourself!\n\n## License\n\nMIT. Made with 💖\n\n<!-- Badges -->\n\n[npm-version-src]: https://img.shields.io/npm/v/destr?style=flat&colorA=18181B&colorB=F0DB4F\n[npm-version-href]: https://npmjs.com/package/destr\n[npm-downloads-src]: https://img.shields.io/npm/dm/destr?style=flat&colorA=18181B&colorB=F0DB4F\n[npm-downloads-href]: https://npmjs.com/package/destr\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/destr?style=flat&colorA=18181B&colorB=F0DB4F\n[bundle-href]: https://bundlephobia.com/result?p=destr\n[license-src]: https://img.shields.io/github/license/unjs/destr.svg?style=flat&colorA=18181B&colorB=F0DB4F\n[license-href]: https://github.com/unjs/destr/blob/main/LICENSE\n","publish_time":1743698475624,"_cnpm_publish_time":1743698475624,"dist-tags":{"latest":"2.0.5"}}