Permalink
Browse files

add stringify

  • Loading branch information...
1 parent 14362c9 commit dd916d7920764460568c904b1f51f2aa5fd81bb1 @dominictarr committed Sep 23, 2011
Showing with 168 additions and 6 deletions.
  1. +42 −2 index.js
  2. +19 −4 readme.markdown
  3. +25 −0 test/parsejson.js
  4. +41 −0 test/stringify.js
  5. +41 −0 test/two-ways.js
View
@@ -8,8 +8,6 @@ var Parser = require('jsonparse')
it makes this code ugly, but his problem is way harder that mine,
so i'll forgive him.
-
-
*/
@@ -67,6 +65,8 @@ exports.parse = function (path) {
stream.readable = true
stream.writable = true
stream.write = function (chunk) {
+ if('string' === typeof chunk)
+ chunk = new Buffer(chunk)
parser.write(chunk)
}
stream.end = function (data) {
@@ -77,4 +77,44 @@ exports.parse = function (path) {
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
}
View
@@ -73,13 +73,28 @@ create a `Stream` that parses the documents from the feed like this:
``` js
JSONStream.parse(['rows', /./, 'doc']) //rows, ANYTHING, doc
-```
-
+```
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
View
@@ -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])))
+
+
+
+}
View
@@ -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')
+ })
+ )
View
@@ -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.