{"id":13447399,"url":"https://github.com/pigulla/json-strictify","last_synced_at":"2025-07-26T19:31:59.417Z","repository":{"id":20846234,"uuid":"24132557","full_name":"pigulla/json-strictify","owner":"pigulla","description":"Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.","archived":false,"fork":false,"pushed_at":"2024-11-18T18:01:00.000Z","size":2616,"stargazers_count":19,"open_issues_count":1,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-18T19:00:40.294Z","etag":null,"topics":["javascript","json","serialization"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pigulla.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-09-17T06:19:03.000Z","updated_at":"2024-11-18T18:00:05.000Z","dependencies_parsed_at":"2023-12-18T08:26:17.757Z","dependency_job_id":"f9dd348c-5bb8-4c31-b47b-cd6ba68dcca2","html_url":"https://github.com/pigulla/json-strictify","commit_stats":{"total_commits":457,"total_committers":8,"mean_commits":57.125,"dds":0.3588621444201313,"last_synced_commit":"b048a4a0056af5965abfcbc8faa12fafc335b5e9"},"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pigulla%2Fjson-strictify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pigulla%2Fjson-strictify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pigulla%2Fjson-strictify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pigulla%2Fjson-strictify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pigulla","download_url":"https://codeload.github.com/pigulla/json-strictify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227707670,"owners_count":17807538,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","json","serialization"],"created_at":"2024-07-31T05:01:16.519Z","updated_at":"2024-12-02T10:19:41.754Z","avatar_url":"https://github.com/pigulla.png","language":"TypeScript","readme":"![Typescript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-blue.svg?style=flat-square)\n[![npm](https://img.shields.io/npm/v/json-strictify?style=flat-square\u0026cacheSeconds=3600)](https://www.npmjs.com/package/json-strictify)\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/pigulla/json-strictify/build-and-test.yml?style=flat-square)\n[![GitHub Issues](https://img.shields.io/github/issues-raw/pigulla/json-strictify?style=flat-square\u0026cacheSeconds=3600)](https://github.com/pigulla/json-strictify/issues)\n[![libraries.io](https://img.shields.io/librariesio/release/npm/json-strictify?style=flat-square\u0026cacheSeconds=3600)](https://libraries.io/npm/json-strictify)\n[![Codecov](https://img.shields.io/codecov/c/github/pigulla/json-strictify?style=flat-square\u0026cacheSeconds=3600)](https://app.codecov.io/gh/pigulla/json-strictify)\n[![npm bundle size](https://img.shields.io/bundlephobia/min/json-strictify?style=flat-square\u0026cacheSeconds=3600)](https://bundlephobia.com/package/json-strictify)\n\n# json-strictify\n\n\u003e Safely serialize a value to JSON without unintended loss of data or going into an infinite loop due to circular references.\n\n#### Why\n\nThe native [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) function drops or silently modifies all values that are not supported by the [JSON specification](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf):\n\n```js\nJSON.stringify({ a: 42, b: undefined })\n// returns '{\"a\":42}'\n\nJSON.parse(JSON.stringify(NaN))\n// returns null\n\nJSON.stringify([1, NaN, 3])\n// returns '[1,null,3]'\n```\n\nIn many cases this is not the behaviour you want: relying on the serialization method to clean up your data is error prone and can lead to subtle bugs that are annoying to find. json-strictify helps you to easily avoid these issues with literally a single line of code.\n\nUnlike [`json-stringify-safe`](https://www.npmjs.org/package/json-stringify-safe) it does not attempt to \"fix\" its input but always bails out when it encounters something that would prevent it from being serialized properly.\n\n---\n\n### Installation\n\nSimply install via npm:\n\n```\nnpm install json-strictify\n```\n\n### Usage\n\njson-strictify exposes three methods: `stringify`, `parse` and `enable`, so it can be used as a drop-in replacement for the native JSON object:\n\n```typescript\nimport JSON from 'json-strictify'\n\nJSON.stringify(someObject)\n```\n\nThe `parse` method is simply a reference to the native `JSON.parse` function.\n\n---\n\n### Examples\n\nThe `stringify` function throws an error if the input to be serialized contains invalid values:\n\n```typescript\nimport JSONs from 'json-strictify'\n\nJSONs.stringify({ x: 42, y: NaN })\n// InvalidValueError: Invalid value at /y (NaN is not JSON-serializable)\n```\n\nAlso, if the data you want to stringify contains circular references a `CircularReferenceError` is thrown:\n\n```typescript\nconst data = []\ndata.push(data)\nJSONs.stringify(data)\n// CircularReferenceError: Circular reference found at \"/0\"\n```\n\nThe location of the value that caused the error is given as a [JSON Pointer](http://tools.ietf.org/html/rfc6901) reference.\n\n---\n\n### ESLint integration\n\nIf you want to ensure that all serialization is done through json-strictify you can disable the global `JSON` variable like so:\n\n```json\n\"globals\": {\n    \"JSON\": \"off\"\n}\n```\n\nSee the ESLint documentation on [configuring globals](https://eslint.org/docs/user-guide/configuring#specifying-globals) for details.\n\n---\n\n### Disabling json-strictify\n\nIn production you may not want to have the additional overhead introduced by json-strictify. This can easily be avoided by calling the `enabled` method:\n\n```typescript\nimport JSONs from 'json-strictify'\nconst JSON = JSONs.enabled(config.debug)\n\n// or for older versions of Javascript:\nconst JSON = require('json-strictify').enabled(config.debug)\n```\n\nIf called with a falsy parameter, `enabled` will return an object that delegates directly to the native JSON object so there will be no performance penalty whatsoever.\n\n**Note:** json-strictify is disabled by default if `NODE_ENV` is set to `production` (you may still enable it manually, of course).\n","funding_links":[],"categories":["Packages","JavaScript","包","目录","Miscellaneous","Number"],"sub_categories":["Miscellaneous","其他","杂项"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpigulla%2Fjson-strictify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpigulla%2Fjson-strictify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpigulla%2Fjson-strictify/lists"}