Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
add stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictarr committed Sep 23, 2011
1 parent 14362c9 commit dd916d7
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 6 deletions.
44 changes: 42 additions & 2 deletions index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ var Parser = require('jsonparse')
it makes this code ugly, but his problem is way harder that mine, it makes this code ugly, but his problem is way harder that mine,
so i'll forgive him. so i'll forgive him.
*/ */


Expand Down Expand Up @@ -67,6 +65,8 @@ exports.parse = function (path) {
stream.readable = true stream.readable = true
stream.writable = true stream.writable = true
stream.write = function (chunk) { stream.write = function (chunk) {
if('string' === typeof chunk)
chunk = new Buffer(chunk)
parser.write(chunk) parser.write(chunk)
} }
stream.end = function (data) { stream.end = function (data) {
Expand All @@ -76,5 +76,45 @@ exports.parse = function (path) {
stream.emit('data', stream.root) stream.emit('data', stream.root)
stream.emit('end') stream.emit('end')
} }
return stream
}

exports.stringify = function (op, sep, cl) {
if (op === false){
op = ''
sep = '\n'
cl = ''
} else if (op == null) {

op = '[\n'
sep = '\n,\n'
cl = '\n]\n'

}

//else, what eve you like

var stream = new Stream ()
, first = true
, ended = false
stream.write = function (data) {
var json = JSON.stringify(data)
if(first) { first = false ; stream.emit('data', op + json)}
else stream.emit('data', sep + json)
}
stream.end = function (data) {
console.error('END ****************',JSON.stringify(cl))
if(ended)
return
ended = true
// if(data)
// stream.write(data)
stream.emit('data', cl)

stream.emit('end')
}
stream.writable = true
stream.readable = true

return stream return stream
} }
23 changes: 19 additions & 4 deletions readme.markdown
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,13 +73,28 @@ create a `Stream` that parses the documents from the feed like this:


``` js ``` js
JSONStream.parse(['rows', /./, 'doc']) //rows, ANYTHING, doc JSONStream.parse(['rows', /./, 'doc']) //rows, ANYTHING, doc
``` ```

awesome! awesome!


## todo ## JSONStream.stringify(open='[\n', sep='\n,\n', cl='\n]\n')

Create a writable stream.
By default, `JSONStream.stringify()` will create an array,
but you may pass in custom `open`, `close`, and `seperator` strings.

If you call `JSONStream.stringify(false)` the elements will only be seperated by a newline.

This will still be valid json if you only write one item.
and will still be easy to split with a RegExp in a
different enviroment where a streaming parser is not available.

## numbers

There seem to be occasional problems parsing and unparsing precise numbers.

I have opened an issue here:


* JSONStream.stringify() https://github.com/creationix/jsonparse/issues/2


## Acknowlegements ## Acknowlegements


Expand Down
25 changes: 25 additions & 0 deletions test/parsejson.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@


/*
sometimes jsonparse changes numbers slightly.
*/

var r = Math.random()
, Parser = require('jsonparse')
, p = new Parser()
, assert = require('assert')
, times = 20
while (times --) {

assert.equal(JSON.parse(JSON.stringify(r)), r, 'core JSON')

p.onValue = function (v) {
console.error('parsed', v)
assert.equal(v, r)
}
console.error('correct', r)
p.write (new Buffer(JSON.stringify([r])))



}
41 changes: 41 additions & 0 deletions test/stringify.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,41 @@

var fs = require ('fs')
, join = require('path').join
, file = join(__dirname, 'fixtures','all_npm.json')
, JSONStream = require('../')
, it = require('it-is').style('colour')

function randomObj () {
return (
Math.random () < 0.4
? {hello: 'eonuhckmqjk',
whatever: 236515,
lies: true,
nothing: [null],
stuff: [Math.random(),Math.random(),Math.random()]
}
: ['AOREC', 'reoubaor', {ouec: 62642}, [[[], {}, 53]]]
)
}

var expected = []
, stringify = JSONStream.stringify()
, es = require('event-stream')
, stringified = ''
, called = 0
, count = 10
, ended = false

while (count --)
expected.push(randomObj())

es.connect(
es.readArray(expected),
stringify,
//JSONStream.parse([/./]),
es.writeArray(function (err, lines) {

it(JSON.parse(lines.join(''))).deepEqual(expected)
console.error('PASSED')
})
)
41 changes: 41 additions & 0 deletions test/two-ways.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,41 @@

var fs = require ('fs')
, join = require('path').join
, file = join(__dirname, 'fixtures','all_npm.json')
, JSONStream = require('../')
, it = require('it-is').style('colour')

function randomObj () {
return (
Math.random () < 0.4
? {hello: 'eonuhckmqjk',
whatever: 236515,
lies: true,
nothing: [null],
// stuff: [Math.random(),Math.random(),Math.random()]
}
: ['AOREC', 'reoubaor', {ouec: 62642}, [[[], {}, 53]]]
)
}

var expected = []
, stringify = JSONStream.stringify()
, es = require('event-stream')
, stringified = ''
, called = 0
, count = 10
, ended = false

while (count --)
expected.push(randomObj())

es.connect(
es.readArray(expected),
stringify,
JSONStream.parse([/./]),
es.writeArray(function (err, lines) {

it(lines).has(expected)
console.error('PASSED')
})
)

0 comments on commit dd916d7

Please sign in to comment.