{"id":15606650,"url":"https://github.com/faleij/json-stream-stringify","last_synced_at":"2025-04-08T15:06:53.738Z","repository":{"id":46971614,"uuid":"58275000","full_name":"Faleij/json-stream-stringify","owner":"Faleij","description":"JSON.Stringify as a readable stream","archived":false,"fork":false,"pushed_at":"2024-10-05T14:50:04.000Z","size":550,"stargazers_count":75,"open_issues_count":7,"forks_count":14,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T14:09:07.254Z","etag":null,"topics":["es6","es6-javascript","javascript","json","json-stringify","node","nodejs","readable-streams"],"latest_commit_sha":null,"homepage":null,"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/Faleij.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-05-07T16:43:34.000Z","updated_at":"2025-01-20T07:47:06.000Z","dependencies_parsed_at":"2024-03-09T11:23:05.803Z","dependency_job_id":"8de82cf1-9742-4d22-bb36-b69b5601d57d","html_url":"https://github.com/Faleij/json-stream-stringify","commit_stats":{"total_commits":128,"total_committers":5,"mean_commits":25.6,"dds":0.03125,"last_synced_commit":"cea33f4511b5f72b0e4244dd7d0bd1a710f2f261"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Faleij%2Fjson-stream-stringify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Faleij%2Fjson-stream-stringify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Faleij%2Fjson-stream-stringify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Faleij%2Fjson-stream-stringify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Faleij","download_url":"https://codeload.github.com/Faleij/json-stream-stringify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247867361,"owners_count":21009240,"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":["es6","es6-javascript","javascript","json","json-stringify","node","nodejs","readable-streams"],"created_at":"2024-10-03T04:41:27.312Z","updated_at":"2025-04-08T15:06:53.701Z","avatar_url":"https://github.com/Faleij.png","language":"TypeScript","readme":"# JSON Stream Stringify\n\n[![NPM version][npm-image]][npm-url]\n[![NPM Downloads][downloads-image]][downloads-url]\n[![Build Status][travis-image]][travis-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![License][license-image]](LICENSE)\n[![Donate][donate-image]][donate-url]\n\nJSON Stringify as a Readable Stream with rescursive resolving of any readable streams and Promises.\n\n## Important and Breaking Changes in v3.1.0\n\n- Completely rewritten from scratch - again\n- Buffer argument added (Stream will not output data untill buffer size is reached - improves speed)\n- Dropped support for node \u003c7.10.1 - async supporting environment now required\n\n## Main Features\n\n- Promises are rescursively resolved and the result is piped through JsonStreamStringify\n- Streams (Object mode) are recursively read and output as arrays\n- Streams (Non-Object mode) are output as a single string\n- Output is streamed optimally with as small chunks as possible\n- Cycling of cyclical structures and dags using [Douglas Crockfords cycle algorithm](https://github.com/douglascrockford/JSON-js)*\n- Great memory management with reference release after processing and WeakMap/Set reference handling\n- Optimal stream pressure handling\n- Tested and runs on ES5**, ES2015**, ES2016 and later\n- Bundled as UMD and Module\n\n\\* Off by default since v2  \n\\** With [polyfills](#usage)  \n\n## Install\n\n```bash\nnpm install --save json-stream-stringify\n\n# Optional if you need polyfills\n# Make sure to include these if you target NodeJS \u003c=v6 or browsers\nnpm install --save @babel/polyfill @babel/runtime\n```\n\n## Usage\n\nUsing Node v8 or later with ESM / Webpack / Browserify / Rollup\n\n### No Polyfills, TS / ESM\n\n```javascript\nimport { JsonStreamStringify } from 'json-stream-stringify';\n```\n\n### Polyfilled, TS / ESM\n\ninstall @babel/runtime-corejs3 and corejs@3\n\n```javascript\nimport { JsonStreamStringify }  from 'json-stream-stringify/polyfill';\nimport { JsonStreamStringify }  from 'json-stream-stringify/module.polyfill'; // force ESM\n```\n\n### Using Node \u003e=8 / Other ES2015 UMD/CommonJS environments\n\n```javascript\nconst { JsonStreamStringify } = require('json-stream-stringify'); // let module resolution decide UMD or CJS\nconst { JsonStreamStringify } = require('json-stream-stringify/umd'); // force UMD\nconst { JsonStreamStringify } = require('json-stream-stringify/cjs'); // force CJS\n```\n\n### Using Node \u003c=6 / Other ES5 UMD/CommonJS environments\n\n```javascript\nconst { JsonStreamStringify } = require('json-stream-stringify/polyfill');\nconst { JsonStreamStringify } = require('json-stream-stringify/umd/polyfill');\nconst { JsonStreamStringify } = require('json-stream-stringify/cjs/polyfill');\n```\n\n**Note:** This library is primarily written for LTS versions of NodeJS. Other environments are not tested.  \n**Note on non-NodeJS usage:** This module depends on node streams library. Any Streams3 compatible implementation should work - as long as it exports a `Readable` class, with instances that looks like readable streams.  \n**Note on Polyfills:** I have taken measures to minify global pollution of polyfills but this library **does not load polyfills by default** because the polyfills modify native object prototypes and it goes against the [W3C recommendations](https://www.w3.org/2001/tag/doc/polyfills/#advice-for-library-and-framework-authors).\n\n## API\n\n### `new JsonStreamStringify(value[, replacer[, spaces[, cycle[, bufferSize=512]]]])`  \n\nStreaming conversion of ``value`` to JSON string.\n\n#### Parameters\n\n- ``value`` ``Any``  \n  Data to convert to JSON.\n\n- ``replacer`` Optional ``Function(key, value)`` or ``Array``  \n  As a function the returned value replaces the value associated with the key. [Details](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter)  \n As an array all other keys are filtered. [Details](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Example_with_an_array)\n\n- ``spaces`` Optional ``String`` or ``Number``  \n  A String or Number object that's used to insert white space into the output JSON string for readability purposes. If this is a Number, it indicates the number of space characters to use as white space. If this is a String, the string is used as white space. If this parameter is not recognized as a finite number or valid string, no white space is used.\n\n- ``cycle`` Optional ``Boolean``  \n  ``true`` enables cycling of cyclical structures and dags.  \n  To restore cyclical structures; use [Crockfords Retrocycle method](https://github.com/douglascrockford/JSON-js) on the parsed object (not included in this module).\n\n#### Returns\n\n- ``JsonStreamStringify`` object that exposes a [Streams3 interface](https://nodejs.org/api/stream.html#stream_class_stream_readable).\n\n### jsonStreamStringify#path\n\nGet current path begin serialized.\n\n#### Returns\n\n- ``Array[String, Number]``  \n  Array of path Strings (keys of objects) and Numbers (index into arrays).  \n  Can be transformed into an mpath with ``.join('.')``.  \n  Useful in conjunction with ``.on('error', ...)``, for figuring out what path may have caused the error.\n\n## Complete Example\n\n```javascript\nconst { JsonStreamStringify } = require('json-stream-stringify');\n\nconst jsonStream = new JsonStreamStringify({\n    // Promises and Streams may resolve more promises and/or streams which will be consumed and processed into json output\n    aPromise: Promise.resolve(Promise.resolve(\"text\")),\n    aStream: ReadableObjectStream({a:1}, 'str'),\n    arr: [1, 2, Promise.resolve(3), Promise.resolve([4, 5]), ReadableStream('a', 'b', 'c')],\n    date: new Date(2016, 0, 2)\n});\njsonStream.once('error', () =\u003e console.log('Error at path', jsonStream.stack.join('.')));\njsonStream.pipe(process.stdout);\n```\n\nOutput (each line represents a write from jsonStreamStringify)\n\n```text\n{\n\"aPromise\":\n\"text\"\n\"aStream\":\n[\n{\n\"a\":\n1\n}\n,\n\"str\"\n]\n\"arr\":\n[\n1\n,\n2\n,\n3\n,\n[\n4\n,\n5\n]\n,\n\"\na\nb\nc\n\"\n],\n\"date\":\n\"2016-01-01T23:00:00.000Z\"\n}\n```\n\n## Practical Example with Express + Mongoose\n\n```javascript\napp.get('/api/users', (req, res, next) =\u003e {\n  res.type('json'); // Required for proper handling by test frameworks and some clients\n  new JsonStreamStringify(Users.find().stream()).pipe(res);\n});\n```\n\n### Why do I not get proper typings? (Missing .on(...), etc.)\n\ninstall ``@types/readable-stream`` or ``@types/node`` or create your own ``stream.d.ts`` that exports a ``Readable`` class.\n\n## License\n\n[MIT](LICENSE)\n\nCopyright (c) 2016 Faleij [faleij@gmail.com](mailto:faleij@gmail.com)\n\n[npm-image]: http://img.shields.io/npm/v/json-stream-stringify.svg\n[npm-url]: https://npmjs.org/package/json-stream-stringify\n[downloads-image]: https://img.shields.io/npm/dm/json-stream-stringify.svg\n[downloads-url]: https://npmjs.org/package/json-stream-stringify\n[travis-image]: https://travis-ci.org/Faleij/json-stream-stringify.svg?branch=master\n[travis-url]: https://travis-ci.org/Faleij/json-stream-stringify\n[coveralls-image]: https://coveralls.io/repos/Faleij/json-stream-stringify/badge.svg?branch=master\u0026service=github\n[coveralls-url]: https://coveralls.io/github/Faleij/json-stream-stringify?branch=master\n[license-image]: https://img.shields.io/badge/license-MIT-blue.svg\n[donate-image]: https://img.shields.io/badge/Donate-PayPal-green.svg\n[donate-url]: https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=faleij%40gmail%2ecom\u0026lc=GB\u0026item_name=faleij\u0026item_number=jsonStreamStringify\u0026currency_code=SEK\u0026bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted\n","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_donations\u0026business=faleij%40gmail%2ecom\u0026lc=GB\u0026item_name=faleij\u0026item_number=jsonStreamStringify\u0026currency_code=SEK\u0026bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHosted"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaleij%2Fjson-stream-stringify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaleij%2Fjson-stream-stringify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaleij%2Fjson-stream-stringify/lists"}