Skip to content

Commit

Permalink
0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
max-mapper committed Aug 30, 2013
0 parents commit 2cd4084
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var through = require('through')
var split = require('split')
var EOL = require('os').EOL

module.exports = parse
module.exports.serialize = serialize
module.exports.parse = parse

function parse() {
return split(function(row) {
if (row) return JSON.parse(row)
})
}

function serialize() {
return through(function(obj) {
this.queue(JSON.stringify(obj) + EOL)
})
}
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "ldjson",
"version": "0.0.1",
"description": "streaming line delimited json parser + serializer",
"main": "index.js",
"scripts": {},
"author": "max ogden",
"license": "BSD",
"dependencies": {
"split": "~0.2.10",
"through": "~2.3.4"
}
}
49 changes: 49 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# ldjson

#### streaming line delimited json parser + serializer

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

## usage

#### ldjson.parse()

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

example newline delimited json:

`data.txt`:

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

usage:

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

#### ldjson.serialize()

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

example usage:

```js
var serialize = require('ldjson').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

0 comments on commit 2cd4084

Please sign in to comment.