# json-stream2

> JSON.parse and JSON.stringify wrapped in a node.js stream

Latest version **0.0.3** (published 2013-10-23) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install json-stream2
pnpm add json-stream2
yarn add json-stream2
bun add json-stream2
```

## Health

**Score 5/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2013-10-23 |
| First published | 2013-10-22 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jason Kuhrt |
| Maintainers | jasonkuhrt |
| Keywords | json, stream |

## Links

- npm: https://www.npmjs.com/package/json-stream2
- Repository: https://github.com/jasonkuhrt/json-stream
- Issues: https://github.com/jasonkuhrt/json-stream/issues
- npm.io page: https://npm.io/package/json-stream2

## Dependencies (3)

- [bun](https://npm.io/package/bun.md) 0.0.10
- [tstream](https://npm.io/package/tstream.md) git://github.com/jasonkuhrt/tstream
- [delimit-stream](https://npm.io/package/delimit-stream.md) 0.0.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.3 (latest) — 2013-10-23
- 0.0.2 — 2013-10-22

## README

# json-stream [![Build Status](https://travis-ci.org/jasonkuhrt/json-stream.png)](https://travis-ci.org/jasonkuhrt/json-stream) [![Dependency Status](https://gemnasium.com/jasonkuhrt/json-stream.png)](https://gemnasium.com/jasonkuhrt/json-stream)
JSON.parse and JSON.stringify wrapped in a node.js stream

## Install
    npm install json-stream2

## Example
```js
var net = require('net');
var jsonStream = require('json-stream');


net.createServer(function(socket){
  // Create in/out json streams
  var parseStream = jsonStream.Parse('\r\n');
  var stringifyStream = jsonStream.Stringify('\r\n');

  // re-wire socket write
  socket.write_ = socket.write;
  socket.write = stringifyStream.write.bind(stringifyStream);
  stringifyStream.pipe(socket);

  // read json, yay!
  socket
  .pipe(parseStream)
  .on('data', function(json){
    console.log('From socket: %j', json);
  });

  // write json, yay!
  socket.write({ Hello: 'Socket!' });
});
```

## API
### .Stringify(delimiter)
```
@param delimiter
  The character(s) that will be appended to the end of each json string to deliniate messages.
```
```
@return
  Stream instance (that inherits from stream.Transform)
```

### .Parse(delimiter)
```
@param delimiter
  What character(s) deliniate messages. The stream is split on this delimiter, the resulting message is put through JSON.parse.
```
```
@return
  Stream instance (that inherits from stream.Transform)
  The stream's objectMode is set to true, thus readers will get javascript objects from this stream.
  @event 'warn'
    When a JSON.parse error occurs it is emitted as a 'warn' event.
    The event handler receives the err object.
```

## Notes
#### Why a `warn` event on `json_stream.Parse`?
If `error` is emitted on a stream it kills the stream. This is too harsh. A server might pipe a socket connection through json_stream.parse. One bad message does not indicate all will be bad. Instead we silently drop messages that fail JSON parsing, and we emit that JSON error as a 'warn' event which won't kill the stream but might let the app handle the problem in some useful way (notifying the peer, logging, etc.).

---
_Source: https://npm.io/package/json-stream2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
