Created
January 20, 2015 02:16
-
-
Save JonGretar/fd9dfc1382f094ad2977 to your computer and use it in GitHub Desktop.
Bufferize support for stream-json
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
'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