4

I want to stringify a large object (to write it to a file) and I'm runnning into v8's string length limit. So I think I either need to find a way to stringify into an ArrayBuffer or I need to create the string in chunks.

It seems https://github.com/dominictarr/JSONStream can do the latter, but I don't understand how to use stringify() as only the usage of .parse() is explained in the documentation.

AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • 1
    You took a look at this : http://stackoverflow.com/questions/32427890/how-to-use-streams-to-json-stringify-large-nested-objects-in-node-js ? – Rabea Jan 10 '16 at 16:16

1 Answers1

4

Here's one way to use JSONStream but it's not an easy task to create the perfect test object so give it a try:

var JSONStream = require('JSONStream');
var es = require('event-stream');
var fs = require('fs');

var obj = {};
for (var i = 0; i < 2000; i++) {
    obj['prop' + i] = 'value' + i;
}

var out = fs.createWriteStream(__dirname + '/out.json');

es.readable(function (count, next) {
    for (var key in obj) {
        this.emit('data', [key, obj[key]]);
    }
    this.emit('end');
    next();
}).pipe(JSONStream.stringifyObject()).pipe(out);
Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • 1
    Malicious code was found in `event-stream` package, so be careful. More information: https://snyk.io/blog/malicious-code-found-in-npm-package-event-stream – Nurzhan Nogerbek Nov 29 '18 at 04:33