summaryrefslogtreecommitdiff
path: root/node_modules/ldjson-stream/readme.md
blob: d6a99c55da20646a7b1c915715dd596b3e717855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# ldjson-stream

#### streaming line delimited json parser + serializer

[![NPM](https://nodei.co/npm/ldjson-stream.png)](https://nodei.co/npm/ldjson-stream/)

## usage

```
var ldj = require('ldjson-stream')
```

#### ldj.parse()

returns a transform stream that accepts newline delimited json and emits objects

example newline delimited json:

`data.txt`:

```
{"foo": "bar"}
{"hello": "world"}
```

If you want to discard non-valid JSON messages, you can call `ldj.parse({strict: false})`

usage:

```js
fs.createReadStream('data.txt')
  .pipe(ldj.parse())
  .on('data', function(obj) {
    // obj is a javascript object
  })
```

#### ldj.serialize()

returns a transform stream that accepts json objects and emits newline delimited json

example usage:

```js
var serialize = ldj.serialize()
serialize.on('data', function(line) {
  // line is a line of stringified JSON with a newline delimiter at the end
})
serialize.write({"foo": "bar"})
serialize.end()
```

### license

BSD