manidlou / concatjson Goto Github PK
View Code? Open in Web Editor NEWconcatenated JSON streaming parser/serializer for node.js
License: MIT License
concatenated JSON streaming parser/serializer for node.js
License: MIT License
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()
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()
+ })
+ })
+})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.