[Pkg-javascript-commits] [node-jsonstream] 05/214: add stringify

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 12:58:32 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-jsonstream.

commit dd916d7920764460568c904b1f51f2aa5fd81bb1
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Fri Sep 23 22:38:34 2011 +1000

    add stringify
---
 index.js          | 44 ++++++++++++++++++++++++++++++++++++++++++--
 readme.markdown   | 23 +++++++++++++++++++----
 test/parsejson.js | 25 +++++++++++++++++++++++++
 test/stringify.js | 41 +++++++++++++++++++++++++++++++++++++++++
 test/two-ways.js  | 41 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 168 insertions(+), 6 deletions(-)

diff --git a/index.js b/index.js
index 2f56246..c8b9499 100644
--- a/index.js
+++ b/index.js
@@ -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
 }
\ No newline at end of file
diff --git a/readme.markdown b/readme.markdown
index 80982f7..b19ccc4 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -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
 
diff --git a/test/parsejson.js b/test/parsejson.js
new file mode 100644
index 0000000..c509860
--- /dev/null
+++ b/test/parsejson.js
@@ -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])))
+
+
+
+}
\ No newline at end of file
diff --git a/test/stringify.js b/test/stringify.js
new file mode 100644
index 0000000..b6de85e
--- /dev/null
+++ b/test/stringify.js
@@ -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')
+    })
+  )
diff --git a/test/two-ways.js b/test/two-ways.js
new file mode 100644
index 0000000..8f3b89c
--- /dev/null
+++ b/test/two-ways.js
@@ -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')
+    })
+  )

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsonstream.git



More information about the Pkg-javascript-commits mailing list