Skip to content

Instantly share code, notes, and snippets.

@JonGretar
Created January 20, 2015 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonGretar/fd9dfc1382f094ad2977 to your computer and use it in GitHub Desktop.
Save JonGretar/fd9dfc1382f094ad2977 to your computer and use it in GitHub Desktop.
Bufferize support for stream-json
'use strict';
var util = require("util");
var Transform = require("stream").Transform;
function Bufferize(options){
Transform.call(this, options);
this._writableState.objectMode = true;
this._readableState.objectMode = false;
this._firstItem = true;
}
util.inherits(Bufferize, Transform);
Bufferize.prototype._transform = function transform(chunk, encoding, done) {
console.log('Transforming', chunk);
switch(chunk.name) {
case 'startObject':
this._firstItem = true;
this.push('{');
break;
case 'endObject':
this.push('}');
break;
case 'startArray':
this.push('[');
break;
case 'startNumber':
this.push(' ');
break;
case 'endArray':
this.push(']');
break;
case 'startString':
// TODO: Find out of there should be a comma here
this.push(' "');
break;
case 'endString':
this.push('"');
break;
case 'startKey':
if (!this._firstItem) { this.push(','); }
this._firstItem = false;
this.push(' "');
break;
case 'endKey':
this.push('":');
break;
case 'numberChunk':
case 'stringChunk':
if (chunk.value) { this.push(chunk.value); }
break;
case 'nullValue':
this.push(' null');
break;
case 'trueValue':
this.push(' true');
break;
case 'falseValue':
this.push(' false');
break;
}
done();
};
// Bufferize.prototype._flush = function (argument) {
// // body...
// }
module.exports = Bufferize;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment