{"id":18917487,"url":"https://github.com/aikoven/json-stringifier","last_synced_at":"2026-04-19T02:05:04.366Z","repository":{"id":57285300,"uuid":"162968783","full_name":"aikoven/json-stringifier","owner":"aikoven","description":"Alternative to JSON.stringify() that supports altering the behavior of the stringification process at string level","archived":false,"fork":false,"pushed_at":"2018-12-24T08:40:46.000Z","size":39,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-14T22:09:42.638Z","etag":null,"topics":["cache","json","memoize","stringifier","stringify"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aikoven.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-24T08:30:52.000Z","updated_at":"2021-01-23T11:03:08.000Z","dependencies_parsed_at":"2022-09-08T21:42:50.114Z","dependency_job_id":null,"html_url":"https://github.com/aikoven/json-stringifier","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aikoven%2Fjson-stringifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aikoven%2Fjson-stringifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aikoven%2Fjson-stringifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aikoven%2Fjson-stringifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aikoven","download_url":"https://codeload.github.com/aikoven/json-stringifier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239914929,"owners_count":19717759,"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":["cache","json","memoize","stringifier","stringify"],"created_at":"2024-11-08T10:26:29.372Z","updated_at":"2026-04-19T02:05:04.324Z","avatar_url":"https://github.com/aikoven.png","language":"TypeScript","readme":"# json-stringifier [![npm version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]\n\nAlternative to `JSON.stringify()` that supports altering the behavior of the\nstringification process at string level.\n\n## Rationale\n\nIt's common to use objects in immutable fashion. We could optimize the\nserialization of these objects by caching their JSON representation. However,\nthere's no way to achieve this using built-in `JSON.stringify()` function: its\n[`replacer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)\nparameter only allows substituting serialized values, but not resulting strings.\n`stringify()` function provided by this library accepts a `stringifier`\nparameter that lets us override the stringification of values in the object\ntree. See [Memoization](#Memoization) example.\n\nAnother use case is when you have a strict schema for some objects inside your\nobject tree. With this library you can use\n[`fast-json-stringify`](https://github.com/fastify/fast-json-stringify) for\nthese objects and the regular stringification for the rest.\n\n## Installation\n\n    $ npm install json-stringifier\n\n## Examples\n\n### Memoization\n\nCustom stringifier that memoizes JSON representations of objects:\n\n```js\nimport {stringify} from 'json-stringifier';\n\nconst cache = new WeakMap();\n\nfunction memoizedStringify(value) {\n  if (value !== null \u0026\u0026 typeof value === 'object') {\n    if (cache.has(value)) {\n      return cache.get(value);\n    } else {\n      const json = stringify(value, memoizedStringify);\n\n      cache.set(value, json);\n\n      return json;\n    }\n  }\n\n  return stringify(value, memoizedStringify);\n}\n\nlet state = {\n  obj: {a: 1},\n  arr: [1, 2, 3],\n};\n\nmemoizedStringify(state); // '{\"obj\":{\"a\":1},\"arr\":[1,2,3]}'\n\nstate = {\n  ...state,\n  arr: [4, 5, 6],\n};\n\nmemoizedStringify(state); // state.obj stringification is bypassed\n```\n\n### Handling Circular References\n\nCustom stringifier that handles circular references:\n\n```js\nfunction safeStringify(value, seen) {\n  if (value !== null \u0026\u0026 typeof value === 'object') {\n    if (seen \u0026\u0026 seen.has(value)) {\n      return '\"\u003ccircular\u003e\"';\n    }\n\n    if (seen == null) {\n      seen = new Set();\n    }\n    seen.add(value);\n\n    const json = stringify(value, child =\u003e safeStringify(child, seen));\n\n    seen.delete(value);\n\n    return json;\n  }\n\n  return stringify(value);\n}\n\nconst obj = {};\nobj.self = obj;\nobj.child = {parent: obj};\n\nsafeStringify(obj); // '{\"self\":\"\u003ccircular\u003e\",\"child\":{\"parent\":\"\u003ccircular\u003e\"}}'\n```\n\n### Support Additional Structures\n\nCustom stringifier that supports Sets and Maps:\n\n```js\nfunction customStringify(value) {\n  if (value instanceof Set) {\n    return stringify({'@@type': 'Set', values: [...value]}, customStringify);\n  }\n\n  if (value instanceof Map) {\n    return stringify({'@@type': 'Map', entries: [...value]}, customStringify);\n  }\n\n  return stringify(value, customStringify);\n}\n\ncustomStringify({\n  set: new Set([1, 2, 3]),\n  map: new Map([[1, 'a'], [2, 'b'], [3, 'c']])),\n});\n// '{\"set\":{\"@@type\":\"Set\",\"values\":[1,2,3]},\"map\":{\"@@type\":\"Map\",\"entries\":[[1,\"a\"],[2,\"b\"],[3,\"c\"]]}}'\n```\n\n## API\n\n`stringifier(value, stringify = stringifier)`\n\n- `value` — The value to convert to a JSON string.\n- `stringify` (optional) — A function that is called to get the JSON string for\n  each property value when `value` is an object, or each element when `value` is\n  an array. `stringify` is called with a single argument — the property value or\n  array element and must return a string or `undefined`. Note that `stringify`\n  is not called with the `value` itself. Defaults to `stringifier`, which gives\n  recursive stringification.\n- Returns a JSON string representing the given value or `undefined` if `value`\n  is `undefined`, a function or a symbol.\n\n## Comparison with JSON.stringify()\n\n`stringify(value)` behaves mostly the same as `JSON.stringify(value)` with few\nexceptions:\n\n- [`toJSON()`](\u003chttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior\u003e)\n  is always called with no arguments.\n- Primitive wrapper types `Boolean`, `Number` and `String` are not supported\n  (yet).\n\n[npm-image]: https://badge.fury.io/js/json-stringifier.svg\n[npm-url]: https://badge.fury.io/js/json-stringifier\n[travis-image]: https://travis-ci.org/aikoven/json-stringifier.svg?branch=master\n[travis-url]: https://travis-ci.org/aikoven/json-stringifier\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faikoven%2Fjson-stringifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faikoven%2Fjson-stringifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faikoven%2Fjson-stringifier/lists"}