Skip to content

Commit 5869138

Browse files
authored
Merge pull request #27 from SimonFrings/naming
Improve documentation and examples
2 parents 327b2f9 + 9c23823 commit 5869138

File tree

3 files changed

+41
-47
lines changed

3 files changed

+41
-47
lines changed

README.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# clue/reactphp-ndjson
22

3-
[![CI status](https://github.com/clue/reactphp-ndjson/workflows/CI/badge.svg)](https://github.com/clue/reactphp-ndjson/actions)
3+
[![CI status](https://github.com/clue/reactphp-ndjson/actions/workflows/ci.yml/badge.svg)](https://github.com/clue/reactphp-ndjson/actions)
44
[![installs on Packagist](https://img.shields.io/packagist/dt/clue/ndjson-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/ndjson-react)
55

66
Streaming newline-delimited JSON ([NDJSON](http://ndjson.org/)) parser and encoder for [ReactPHP](https://reactphp.org/).
@@ -10,7 +10,7 @@ file to store any kind of (uniform) structured data, such as a list of user
1010
objects or log entries. It uses a simple newline character between each
1111
individual record and as such can be both used for efficient persistence and
1212
simple append-style operations. This also allows it to be used in a streaming
13-
context, such as a simple inter-process commmunication (IPC) protocol or for a
13+
context, such as a simple inter-process communication (IPC) protocol or for a
1414
remote procedure call (RPC) mechanism. This library provides a simple
1515
streaming API to process very large NDJSON files with thousands or even millions
1616
of rows efficiently without having to load the whole file into memory at once.
@@ -39,7 +39,7 @@ of rows efficiently without having to load the whole file into memory at once.
3939

4040
## Support us
4141

42-
We invest a lot of time developing, maintaining and updating our awesome
42+
We invest a lot of time developing, maintaining, and updating our awesome
4343
open-source projects. You can help us sustain this high-quality of our work by
4444
[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Sponsors get
4545
numerous benefits in return, see our [sponsoring page](https://github.com/sponsors/clue)
@@ -75,7 +75,7 @@ no "outer array" to be modified). This makes it a perfect fit for a streaming
7575
context, for line-oriented CLI tools (such as `grep` and others) or for a logging
7676
context where you want to append records at a later time. Additionally, this
7777
also allows it to be used in a streaming context, such as a simple inter-process
78-
commmunication (IPC) protocol or for a remote procedure call (RPC) mechanism.
78+
communication (IPC) protocol or for a remote procedure call (RPC) mechanism.
7979

8080
The newline character at the end of each line allows for some really simple
8181
*framing* (detecting individual records). While each individual line is valid
@@ -134,12 +134,12 @@ as parsed values instead of just chunks of strings:
134134
```
135135

136136
```php
137-
$stdin = new ReadableResourceStream(STDIN);
137+
$stdin = new React\Stream\ReadableResourceStream(STDIN);
138138

139-
$stream = new Decoder($stdin);
139+
$ndjson = new Clue\React\NDJson\Decoder($stdin);
140140

141-
$stream->on('data', function ($data) {
142-
// data is a parsed element from the JSON stream
141+
$ndjson->on('data', function ($data) {
142+
// $data is a parsed element from the JSON stream
143143
// line 1: $data = (object)array('name' => 'test', 'active' => true);
144144
// line 2: $data = (object)array('name' => 'hello wörld', 'active' => true);
145145
var_dump($data);
@@ -157,9 +157,9 @@ This means that, by default, JSON objects will be emitted as a `stdClass`.
157157
This behavior can be controlled through the optional constructor parameters:
158158

159159
```php
160-
$stream = new Decoder($stdin, true);
160+
$ndjson = new Clue\React\NDJson\Decoder($stdin, true);
161161

162-
$stream->on('data', function ($data) {
162+
$ndjson->on('data', function ($data) {
163163
// JSON objects will be emitted as assoc arrays now
164164
});
165165
```
@@ -171,15 +171,15 @@ unreasonably long lines. It accepts an additional argument if you want to change
171171
this from the default of 64 KiB:
172172

173173
```php
174-
$stream = new Decoder($stdin, false, 512, 0, 64 * 1024);
174+
$ndjson = new Clue\React\NDJson\Decoder($stdin, false, 512, 0, 64 * 1024);
175175
```
176176

177177
If the underlying stream emits an `error` event or the plain stream contains
178178
any data that does not represent a valid NDJson stream,
179179
it will emit an `error` event and then `close` the input stream:
180180

181181
```php
182-
$stream->on('error', function (Exception $error) {
182+
$ndjson->on('error', function (Exception $error) {
183183
// an error occured, stream will close next
184184
});
185185
```
@@ -190,7 +190,7 @@ followed by an `end` event on success or an `error` event for
190190
incomplete/invalid JSON data as above:
191191

192192
```php
193-
$stream->on('end', function () {
193+
$ndjson->on('end', function () {
194194
// stream successfully ended, stream will close next
195195
});
196196
```
@@ -199,7 +199,7 @@ If either the underlying stream or the `Decoder` is closed, it will forward
199199
the `close` event:
200200

201201
```php
202-
$stream->on('close', function () {
202+
$ndjson->on('close', function () {
203203
// stream closed
204204
// possibly after an "end" event or due to an "error" event
205205
});
@@ -209,7 +209,7 @@ The `close(): void` method can be used to explicitly close the `Decoder` and
209209
its underlying stream:
210210

211211
```php
212-
$stream->close();
212+
$ndjson->close();
213213
```
214214

215215
The `pipe(WritableStreamInterface $dest, array $options = array(): WritableStreamInterface`
@@ -218,7 +218,7 @@ Please note that the `Decoder` emits decoded/parsed data events, while many
218218
(most?) writable streams expect only data chunks:
219219

220220
```php
221-
$stream->pipe($logger);
221+
$ndjson->pipe($logger);
222222
```
223223

224224
For more details, see ReactPHP's
@@ -234,12 +234,12 @@ and accepts its data through the same interface, but handles any data as complet
234234
JSON elements instead of just chunks of strings:
235235

236236
```php
237-
$stdout = new WritableResourceStream(STDOUT);
237+
$stdout = new React\Stream\WritableResourceStream(STDOUT);
238238

239-
$stream = new Encoder($stdout);
239+
$ndjson = new Clue\React\NDJson\Encoder($stdout);
240240

241-
$stream->write(array('name' => 'test', 'active' => true));
242-
$stream->write(array('name' => 'hello wörld', 'active' => true));
241+
$ndjson->write(array('name' => 'test', 'active' => true));
242+
$ndjson->write(array('name' => 'hello wörld', 'active' => true));
243243
```
244244
```
245245
{"name":"test","active":true}
@@ -248,13 +248,13 @@ $stream->write(array('name' => 'hello wörld', 'active' => true));
248248

249249
The `Encoder` supports the same parameters as the underlying
250250
[`json_encode()`](https://www.php.net/manual/en/function.json-encode.php) function.
251-
This means that, by default, unicode characters will be escaped in the output.
251+
This means that, by default, Unicode characters will be escaped in the output.
252252
This behavior can be controlled through the optional constructor parameters:
253253

254254
```php
255-
$stream = new Encoder($stdout, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
255+
$ndjson = new Clue\React\NDJson\Encoder($stdout, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
256256

257-
$stream->write('hello wörld');
257+
$ndjson->write('hello wörld');
258258
```
259259
```
260260
"hello wörld"
@@ -268,7 +268,7 @@ any data that can not be represented as a valid NDJSON stream,
268268
it will emit an `error` event and then `close` the input stream:
269269

270270
```php
271-
$stream->on('error', function (Exception $error) {
271+
$ndjson->on('error', function (Exception $error) {
272272
// an error occured, stream will close next
273273
});
274274
```
@@ -277,7 +277,7 @@ If either the underlying stream or the `Encoder` is closed, it will forward
277277
the `close` event:
278278

279279
```php
280-
$stream->on('close', function () {
280+
$ndjson->on('close', function () {
281281
// stream closed
282282
// possibly after an "end" event or due to an "error" event
283283
});
@@ -287,22 +287,22 @@ The `end(mixed $data = null): void` method can be used to optionally emit
287287
any final data and then soft-close the `Encoder` and its underlying stream:
288288

289289
```php
290-
$stream->end();
290+
$ndjson->end();
291291
```
292292

293293
The `close(): void` method can be used to explicitly close the `Encoder` and
294294
its underlying stream:
295295

296296
```php
297-
$stream->close();
297+
$ndjson->close();
298298
```
299299

300300
For more details, see ReactPHP's
301301
[`WritableStreamInterface`](https://github.com/reactphp/stream#writablestreaminterface).
302302

303303
## Install
304304

305-
The recommended way to install this library is [through Composer](https://getcomposer.org).
305+
The recommended way to install this library is [through Composer](https://getcomposer.org/).
306306
[New to Composer?](https://getcomposer.org/doc/00-intro.md)
307307

308308
This project follows [SemVer](https://semver.org/).
@@ -317,12 +317,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
317317
This project aims to run on any platform and thus does not require any PHP
318318
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
319319
HHVM.
320-
It's *highly recommended to use PHP 7+* for this project.
320+
It's *highly recommended to use the latest supported PHP version* for this project.
321321

322322
## Tests
323323

324324
To run the test suite, you first need to clone this repo and then install all
325-
dependencies [through Composer](https://getcomposer.org):
325+
dependencies [through Composer](https://getcomposer.org/):
326326

327327
```bash
328328
$ composer install
@@ -331,7 +331,7 @@ $ composer install
331331
To run the test suite, go to the project root and run:
332332

333333
```bash
334-
$ php vendor/bin/phpunit
334+
$ vendor/bin/phpunit
335335
```
336336

337337
## License

examples/91-benchmark-count.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
// 3) pipe NDJSON into benchmark script:
1515
// $ php examples/91-benchmark-count.php < title.ratings.ndjson
1616

17-
use Clue\React\NDJson\Decoder;
1817
use React\EventLoop\Loop;
19-
use React\Stream\ReadableResourceStream;
2018

2119
require __DIR__ . '/../vendor/autoload.php';
2220

2321
if (extension_loaded('xdebug')) {
2422
echo 'NOTICE: The "xdebug" extension is loaded, this has a major impact on performance.' . PHP_EOL;
2523
}
2624

27-
$decoder = new Decoder(new ReadableResourceStream(STDIN), true);
25+
$ndjson = new Clue\React\NDJson\Decoder(new React\Stream\ReadableResourceStream(STDIN), true);
2826

2927
$count = 0;
30-
$decoder->on('data', function () use (&$count) {
28+
$ndjson->on('data', function () use (&$count) {
3129
++$count;
3230
});
3331

@@ -36,7 +34,7 @@
3634
printf("\r%d records in %0.3fs...", $count, microtime(true) - $start);
3735
});
3836

39-
$decoder->on('close', function () use (&$count, $report, $start) {
37+
$ndjson->on('close', function () use (&$count, $report, $start) {
4038
$now = microtime(true);
4139
Loop::cancelTimer($report);
4240

examples/validate.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22

33
// $ php examples/validate.php < examples/users.ndjson
44

5-
use Clue\React\NDJson\Decoder;
6-
use Clue\React\NDJson\Encoder;
75
use React\EventLoop\Loop;
8-
use React\Stream\ReadableResourceStream;
9-
use React\Stream\WritableResourceStream;
106

117
require __DIR__ . '/../vendor/autoload.php';
128

139
$exit = 0;
14-
$in = new ReadableResourceStream(STDIN);
15-
$out = new WritableResourceStream(STDOUT);
16-
$info = new WritableResourceStream(STDERR);
10+
$in = new React\Stream\ReadableResourceStream(STDIN);
11+
$out = new React\Stream\WritableResourceStream(STDOUT);
12+
$info = new React\Stream\WritableResourceStream(STDERR);
1713

18-
$decoder = new Decoder($in);
19-
$encoder = new Encoder($out);
20-
$decoder->pipe($encoder);
14+
$ndjson = new Clue\React\NDJson\Decoder($in);
15+
$encoder = new Clue\React\NDJson\Encoder($out);
16+
$ndjson->pipe($encoder);
2117

22-
$decoder->on('error', function (Exception $e) use ($info, &$exit) {
18+
$ndjson->on('error', function (Exception $e) use ($info, &$exit) {
2319
$info->write('ERROR: ' . $e->getMessage() . PHP_EOL);
2420
$exit = 1;
2521
});

0 commit comments

Comments
 (0)