-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throws errors when parsing invalid JSON #9
Open
refractalize
wants to merge
6
commits into
mmalecki:master
Choose a base branch
from
featurist:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42f80b7
using json stream example from https://nodejs.org/api/stream.html#str…
refractalize 52add36
test for throwing errors
refractalize e3e8efe
using async all the time
refractalize a656141
removed invalid JSON bit at bottom of readme
refractalize 652f2bb
allow async and non-async options
refractalize e384a69
fixed node 0.10 test failure
refractalize File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,3 @@ Will output: | |
{ a: 42 } | ||
{ hello: 'world' } | ||
``` | ||
|
||
If invalid JSON gets written, it's silently ignored. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,62 @@ | ||
var util = require('util'), | ||
TransformStream = require('stream').Transform; | ||
const util = require('util'); | ||
const StringDecoder = require('string_decoder').StringDecoder; | ||
const Transform = require('stream').Transform; | ||
util.inherits(JSONStream, Transform); | ||
|
||
module.exports = function (options) { | ||
return new JSONStream(options); | ||
}; | ||
|
||
var JSONStream = module.exports.JSONStream = function (options) { | ||
// Gets \n-delimited JSON string data, and emits the parsed objects | ||
function JSONStream(options) { | ||
options = options || {}; | ||
TransformStream.call(this, options); | ||
Transform.call(this, options); | ||
this._writableState.objectMode = false; | ||
this._readableState.objectMode = true; | ||
this._async = options.async || false; | ||
}; | ||
util.inherits(JSONStream, TransformStream); | ||
|
||
JSONStream.prototype._transform = function (data, encoding, callback) { | ||
if (!Buffer.isBuffer(data)) data = new Buffer(data); | ||
if (this._buffer) { | ||
data = Buffer.concat([this._buffer, data]); | ||
this._buffer = ''; | ||
this._decoder = new StringDecoder('utf8'); | ||
this.async = options && options.async? setImmediate: function (fn) { fn(); }; | ||
} | ||
|
||
JSONStream.prototype._transform = function(chunk, encoding, cb) { | ||
this._buffer += this._decoder.write(chunk); | ||
// split on newlines | ||
var lines = this._buffer.split(/\r?\n/); | ||
// keep the last partial line buffered | ||
this._buffer = lines.pop(); | ||
for (var l = 0; l < lines.length; l++) { | ||
var line = lines[l]; | ||
this._pushLine(line); | ||
} | ||
cb(); | ||
}; | ||
|
||
var ptr = 0, start = 0; | ||
while (++ptr <= data.length) { | ||
if (data[ptr] === 10 || ptr === data.length) { | ||
var line; | ||
try { | ||
line = JSON.parse(data.slice(start, ptr)); | ||
} | ||
catch (ex) { } | ||
if (line) { | ||
this.push(line); | ||
line = null; | ||
} | ||
if (data[ptr] === 10) start = ++ptr; | ||
JSONStream.prototype._pushLine = function(line) { | ||
var self = this; | ||
|
||
if (line.trim()) { | ||
try { | ||
var obj = JSON.parse(line); | ||
} catch (er) { | ||
this.async(function () { | ||
self.emit('error', er); | ||
}); | ||
return; | ||
} | ||
// push the parsed object out to the readable consumer | ||
|
||
this.async(function () { | ||
self.push(obj); | ||
}); | ||
} | ||
} | ||
|
||
JSONStream.prototype._flush = function(cb) { | ||
// Just handle any leftover | ||
var rem = this._buffer.trim(); | ||
this._pushLine(rem); | ||
this.async(function () { | ||
cb(); | ||
}); | ||
}; | ||
|
||
this._buffer = data.slice(start); | ||
return this._async | ||
? void setImmediate(callback) | ||
: void callback(); | ||
module.exports = function (options) { | ||
return new JSONStream(options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var assert = require('assert'); | ||
var JSONStream = require('../'); | ||
|
||
var stream = JSONStream(); | ||
|
||
var objects = []; | ||
|
||
stream.on('data', function (data) { | ||
objects.push(data); | ||
}); | ||
|
||
stream.on('error', function (error) { | ||
objects.push(error); | ||
assert.equal(error.message, 'Unexpected token t'); | ||
}); | ||
|
||
stream.on('end', function () { | ||
assert.equal(objects.length, 3); | ||
assert.deepEqual(objects[0], {"this": "is valid JSON"}); | ||
assert.equal(objects[1].message, 'Unexpected token t'); | ||
assert.deepEqual(objects[2], ["this", "is", "valid", "JSON"]); | ||
}); | ||
|
||
stream.write('{"this": "is valid JSON"}\n'); | ||
try { | ||
stream.write('{this is not valid JSON]\n'); | ||
} catch (e) { | ||
} | ||
stream.write('["this", "is", "valid", "JSON"]\n'); | ||
stream.end(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcrugzz so you reckon this bit should do buffer concat, not string concat?