11# clue/reactphp-ndjson [ ![ Build Status] ( https://travis-ci.org/clue/reactphp-ndjson.svg?branch=master )] ( https://travis-ci.org/clue/reactphp-ndjson )
22
3- Streaming newline delimited JSON ([ NDJSON] ( http://ndjson.org/ ) ) parser and encoder, built on top of React PHP
3+ Streaming newline delimited JSON ([ NDJSON] ( http://ndjson.org/ ) ) parser and encoder for ReactPHP.
44
55** Table of Contents**
66
@@ -24,23 +24,24 @@ and exposes its data through the same interface, but emits the JSON elements
2424as parsed values instead of just chunks of strings:
2525
2626```
27- {"name":"test","active":true}\r\n
28- " hello w\u00f6rld"\r\n
27+ {"name":"test","active":true}
28+ {"name":" hello w\u00f6rld","active":true}
2929```
30+
3031``` php
3132$stdin = new ReadableResourceStream(STDIN, $loop);
3233
3334$stream = new Decoder($stdin);
3435
3536$stream->on('data', function ($data) {
36- // data is a parsed element form the JSON stream
37+ // data is a parsed element from the JSON stream
3738 // line 1: $data = (object)array('name' => 'test', 'active' => true);
38- // line 2: $data = " hello wörld" ;
39+ // line 2: $data = (object)array('name' => ' hello wörld', 'active' => true) ;
3940 var_dump($data);
4041});
4142```
4243
43- React 's streams emit chunks of data strings and make no assumption about their lengths.
44+ ReactPHP 's streams emit chunks of data strings and make no assumption about their lengths.
4445These chunks do not necessarily represent complete JSON elements, as an
4546element may be broken up into multiple chunks.
4647This class reassembles these elements by buffering incomplete ones.
@@ -115,7 +116,7 @@ Please note that the `Decoder` emits decoded/parsed data events, while many
115116$stream->pipe($logger);
116117```
117118
118- For more details, see React 's
119+ For more details, see ReactPHP 's
119120[ ` ReadableStreamInterface ` ] ( https://github.com/reactphp/stream#readablestreaminterface ) .
120121
121122### Encoder
@@ -133,11 +134,11 @@ $stdout = new WritableResourceStream(STDOUT, $loop);
133134$stream = new Encoder($stdout);
134135
135136$stream->write(array('name' => 'test', 'active' => true));
136- $stream->write(' hello wörld');
137+ $stream->write(array('name' => ' hello wörld', 'active' => true) );
137138```
138139```
139- {"name":"test","active":true}\r\n
140- " hello w\u00f6rld"\r\n
140+ {"name":"test","active":true}
141+ {"name":" hello w\u00f6rld","active":true}
141142```
142143
143144The ` Encoder ` supports the same parameters as the underlying
@@ -151,13 +152,13 @@ $stream = new Encoder($stdout, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
151152$stream->write('hello wörld');
152153```
153154```
154- "hello wörld"\r\n
155+ "hello wörld"
155156```
156157
157158Note that trying to pass the ` JSON_PRETTY_PRINT ` option will yield an
158159` InvalidArgumentException ` because it is not compatible with NDJSON.
159160
160- If the underlying stream emits an ` error ` event or the given datacontains
161+ If the underlying stream emits an ` error ` event or the given data contains
161162any data that can not be represented as a valid NDJSON stream,
162163it will emit an ` error ` event and then ` close ` the input stream:
163164
@@ -191,10 +192,9 @@ its underlying stream:
191192$stream->close();
192193```
193194
194- For more details, see React 's
195+ For more details, see ReactPHP 's
195196[ ` WritableStreamInterface ` ] ( https://github.com/reactphp/stream#writablestreaminterface ) .
196197
197-
198198## Install
199199
200200The recommended way to install this library is [ through Composer] ( https://getcomposer.org ) .
@@ -203,7 +203,7 @@ The recommended way to install this library is [through Composer](https://getcom
203203This will install the latest supported version:
204204
205205``` bash
206- $ composer require clue/ndjson-react:^0.1.1
206+ $ composer require clue/ndjson-react:^0.1.2
207207```
208208
209209See also the [ CHANGELOG] ( CHANGELOG.md ) for details about version upgrades.
0 commit comments