{"id":15663478,"url":"https://github.com/simone-sanfratello/json-stringify-extended","last_synced_at":"2026-03-11T14:05:55.931Z","repository":{"id":57285315,"uuid":"96990053","full_name":"simone-sanfratello/json-stringify-extended","owner":"simone-sanfratello","description":"JSON.stringify any data types","archived":false,"fork":false,"pushed_at":"2020-12-11T09:24:48.000Z","size":298,"stargazers_count":17,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T00:51:11.363Z","etag":null,"topics":["json","serialization","stringify"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/simone-sanfratello.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":"2017-07-12T09:30:17.000Z","updated_at":"2023-09-14T12:27:25.000Z","dependencies_parsed_at":"2022-09-19T22:41:52.120Z","dependency_job_id":null,"html_url":"https://github.com/simone-sanfratello/json-stringify-extended","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simone-sanfratello%2Fjson-stringify-extended","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simone-sanfratello%2Fjson-stringify-extended/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simone-sanfratello%2Fjson-stringify-extended/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simone-sanfratello%2Fjson-stringify-extended/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simone-sanfratello","download_url":"https://codeload.github.com/simone-sanfratello/json-stringify-extended/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252596320,"owners_count":21773842,"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":["json","serialization","stringify"],"created_at":"2024-10-03T13:37:36.515Z","updated_at":"2026-03-11T14:05:55.901Z","avatar_url":"https://github.com/simone-sanfratello.png","language":"JavaScript","readme":"# json-stringify-extended\n\n[![NPM Version](http://img.shields.io/npm/v/json-stringify-extended.svg?style=flat)](https://www.npmjs.org/package/json-stringify-extended)\n[![NPM Downloads](https://img.shields.io/npm/dm/json-stringify-extended.svg?style=flat)](https://www.npmjs.org/package/json-stringify-extended)\n[![JS Standard Style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n![100% code coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)\n\n\nJSON.stringify any data types\n\n## Purpose\n\n- stringify primitive js types properly without limitation\n- manipulate objects while serializing\n- get control on circular reference\n- use custom types not yet defined (e.g. enums)\n- avoid quotes if not needed\n\n## Installing\n\n````bash\nnpm i json-stringify-extended\n````\n\n### Quick start\n\n```js\nconst stringify = require('json-stringify-extended')\n\nconst data = {\n  a: 'basic set, default options',\n  b: 1,\n  c: true,\n  d: function (a, b) { console.log(a + b) },\n  e: {a: 0, b: 0.1, c: -2},\n  f: ['a', 'b', 'c'],\n  g: new Date('2017-01-01'),\n  h: /a|b/,\n  i: null,\n  j: Infinity,\n  k: NaN,\n  l: undefined,\n  m: stringify.deferred('my.enum.VALUE'),\n  n: Buffer.from('7468697320697320612074c3a97374', 'hex'),\n  o: Symbol('cross'),\n  p: new Map([[1, 'Rico'], [2, 'Mimi']]),\n  q: new Set(['cuori', 'quadri', 'picche', 'fiori'])\n}\n\nconsole.log(stringify(data))\n\n// output\n{\n  a:\"basic set, default options\",\n  b:1,\n  c:true,\n  d:function (a, b) { console.log(a + b) },\n  e:{\n    a:0,\n    b:0.1,\n    c:-2\n  },\n  f:[\n    \"a\",\n    \"b\",\n    \"c\"\n  ],\n  g:new Date(\"2017-01-01T00:00:00.000Z\"),\n  h:/a|b/,\n  i:null,\n  j:Infinity,\n  k:NaN,\n  l:undefined,\n  m:my.enum.VALUE,\n  n:Buffer.from(\"dGhpcyBpcyBhIHTDqXN0\"),\n  o:Symbol(\"cross\"),\n  p:new Map([\n    [\n      1,\n      \"Rico\"\n    ],\n    [\n      2,\n      \"Mimi\"\n    ]\n  ]),\n  q:new Set([\n    \"cuori\",\n    \"quadri\",\n    \"picche\",\n    \"fiori\"\n  ])\n}\n\n```\n\n## Supported types\n\n* ``string``\n* ``number``\n* ``boolean``\n* ``function``\n* ``Object``\n* ``Array``\n* ``Date``\n* ``RegExp``\n* ``Buffer``\n* ``Symbol``\n* ``Map``\n* ``Set``\n* ``null``\n* ``Infinity``\n* ``NaN``\n* ``undefined``\n* Not-yet defined using ``stringify.deferred``\n\n## API docs\n\n### stringify (data[, options])\n\nstringify data into string\n\n#### data\nType: `any`\n\n#### options\nType: `Object`\n\nParameters to configure result format.\n\nYou can also use this prepared options:\n\n- ``stringify.options.json``\n format to regular JSON format\n- ``stringify.options.standadjs``\n format using standadjs rules\n- ``stringify.options.compact``\n format without folding, quotes and spaces\n\n**example**\n````js\nconst data = {a: 'string', b: false, c: [0, 1, 2]}\nconsole.log (stringify (data, stringify.options.compact))\n// output\n{a:'string',b:false,c:[0,1,2]}\n````\n\n##### options.filter\nType: `function(key:string, value:string) return boolean`  \nDefault: `null`\n\nUse the function to filter ``key`` and/or ``value`` of each element.  \nReturn ``true`` to save element or ``false`` to discard.  \n``filter`` applies before ``replace``.  \n\n**example**\n````js\nconst data = {\n user: 'alice',\n password: 'it-s-a-secret',\n id: 1,\n meta: ['1', 1],\n greet: function() { return 'hi' }\n}\nconst options = {\n filter: function (key, value) {\n   if (key === 'password') {\n     return false\n   }\n   if (value === 1) {\n     return false\n   }\n   if (typeof value === 'function') {\n     return false\n   }\n   return true\n }\n}\nconsole.log(stringify(data, options))\n// output\n{\n user:\"alice\",\n meta:[\n   \"1\"\n ]\n}\n````\n\n##### options.replace\nType: `function(key:string, value:string) return {key, value}`  \nDefault: `null`\n\nUse the function to replace ``key`` and/or ``value`` of each element.  \nHave to return an object ``{key, value}``.  \n``filter`` applies before ``replace``.  \n\n**example**\n````js\nconst data = {\n user: 'alice',\n password: 'it-s-a-secret',\n id: 1,\n meta: ['1', 1]\n}\nconst options = {\n replace: function (key, value) {\n   if (key === 'password') {\n     return {key: 'secret', value: '***'}\n   }\n   if (value === 1) {\n     return {key, value: 'one'}\n   }\n   return {key, value}\n }\n}\nconsole.log(stringify(data, options))\n// output\n{\n user:\"alice\",\n secret:\"***\",\n id:\"one\",\n meta:[\n   \"1\",\n   \"one\"\n ]\n}\n````\n\n##### options.safe\nType: `boolean`  \nDefault: `false`\n\nWorks in safe mode, so it will not throws exception for circularity.\n\n##### options.endline\nType: `string`  \nDefault: `\\n`\n\nEndline string should contain spacing chars as `\\n` or `\\r\\n`. Set to empty string `''` for one line output.\n\n##### options.spacing\nType: `string`  \nDefault: `  ` (two spaces)\n\nIndentation string should contains only spacing chars as `\\t` or spaces ` `.\n\n##### options.compress\nType: `boolean`  \nDefault: `false`\n\nCompress data for ``function`` and ``Date``.\nNote: in version \u003c `2.0.0` also discard ``null`` and ``undefined`` values.\n\n##### options.keyQuote\nType: `string`  \nDefault: `null`\n\nThe character to be used for quote is the “ key, the  default is `null`, meaning no quotes in keys. And `\"` or `'` or `null` means there is a quote. \n\n##### options.valueQuote\nType: `string`  \nDefault: `'`\n\nThe character to be used for quote values default is `'`. Must be `\"` or `'`.\n\n##### options.keySpace\nType: `boolean`  \nDefault: `false`\n\nAdd a space between `key:` and `value`.\n\n##### options.discard\nType: `boolean`  \nDefault: `false`\n\nDiscard values `null` and ``undefined``.  \n\n---\n\n## Changelog\n\n#### v. 2.2.0\n- support `Map` and `Set` types\n\n#### v. 2.1.0\n- support `Symbol` type\n- **100%** code coverage\n- drop `node` \u003c `10`\n\n#### v. 2.0.0\n- separate `compress` and `discard` option (to keep object keys on compress)\n\n---\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2017-2020 Simone Sanfratello\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimone-sanfratello%2Fjson-stringify-extended","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimone-sanfratello%2Fjson-stringify-extended","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimone-sanfratello%2Fjson-stringify-extended/lists"}