Created
March 28, 2019 02:25
-
-
Save deleteman/ea1e03aed8e9e6d11fc027a9038bdd6c to your computer and use it in GitHub Desktop.
serialization using jsonstream
This file contains hidden or 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
var fileSystem = require( "fs" ); | |
var JSONStream = require( "JSONStream" ); | |
var books = [ | |
{name: "The Philosopher's Stone", year: 1997}, | |
{name: "The Chamber of Secrets", year: 1998}, | |
{name: "The Prisoner of Azkaban", year: 1999}, | |
{name: "The Goblet of Fire", year:2000}, | |
{name: "The Order of the Phoenix", year:2003}, | |
{name: "The Half-Blood Prince", year:2005}, | |
{name: "The Deathly Hallows", year:2007} | |
]; | |
var transformStream = JSONStream.stringify(); | |
var outputStream = fileSystem.createWriteStream( __dirname + "/hpdata.json" ); | |
transformStream.pipe( outputStream ); | |
books.forEach( transformStream.write ); | |
transformStream.end(); | |
outputStream.on( | |
"finish", | |
function handleFinish() { | |
console.log( "JSONStream serialization complete!" ); | |
} | |
); | |
outputStream.on( | |
"finish", | |
function handleFinish() { | |
var transformStream = JSONStream.parse( "*" ); | |
var inputStream = fileSystem.createReadStream( __dirname + "/data.json" ); | |
inputStream | |
.pipe( transformStream ) | |
.on( | |
"data", | |
function handleRecord( data ) { | |
console.log( "Record (event):" , data ); | |
} | |
) | |
.on( | |
"end", | |
function handleEnd() { | |
console.log( "JSONStream parsing complete!" ); | |
} | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment