GithubHelp home page GithubHelp logo

manidlou / concatjson Goto Github PK

View Code? Open in Web Editor NEW
9.0 9.0 4.0 71 KB

concatenated JSON streaming parser/serializer for node.js

License: MIT License

JavaScript 100.00%
concatenated-json nodejs serializer stream streaming-parser

concatjson's People

Contributors

bcantrill avatar manidlou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

concatjson's Issues

concantjson explodes with strings including a {

concantjson "explodes" i.e. generates an uncaught parser error, when trying to parse {}{"foo":"{"}. Somehow concantjson doesn't handle curly brackets right. And yes, they don't need to be escaped according to RFC 7159, and the subjson {"foo":"{"} is parsed without problems by JSON.parse()

support concatenated JSON delimited with whitespace

Currently, concatenated JSON delimited with whitespace doesn't parse. Here's a diff (apologies if a pull request is preferred) that has a test for this and a proposed fix:

diff --git a/concatjson.js b/concatjson.js
index 2b4755b..30248cc 100644
--- a/concatjson.js
+++ b/concatjson.js
@@ -3,7 +3,7 @@ const thru = require('through2')
 const split = require('split2')
 
 function parse () {
-  return split('}{', parseStream)
+  return split(/}\s*{/, parseStream)
 
   function parseStream (dat) {
     if (!dat) return null
diff --git a/test/test.js b/test/test.js
index d4b88b9..8fb9b7f 100644
--- a/test/test.js
+++ b/test/test.js
@@ -233,3 +233,34 @@ test('+ parse() > more curly brackets in object contents', t => {
     })
   })
 })
+
+test('+ parse() > test whitespace allowed between concatenated objects', t => {
+  const Readable = require('stream').Readable;
+  const out = new Readable;
+  out._read = function noop() {};
+
+  const parser = cj.parse();
+  out.pipe(parser);
+
+  /*
+   * This is the (second) example from the "Concatenated JSON" section from
+   * the Wikipedia article on "JSON Streaming."
+   */
+  out.push('{"some":"thing\\n"}\n');
+  out.push('{"may": {\n');
+  out.push('  "include":"nested",\n');
+  out.push(' "objects":[\n');
+  out.push('      "and","arrays"\n');
+  out.push(' ]\n');
+  out.push('}}\n');
+  out.push(null);
+
+  parser.once('data', dat => {
+    t.equal(dat.some, 'thing\n')
+    parser.once('data', dat => {
+      t.deepEqual(dat.may,
+        { include: 'nested', objects: [ 'and', 'arrays' ] });
+      t.end()
+    })
+  })
+})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.