2017-04-20 73 views
0

我有一个分页的请求,给了我一个对象列表,我稍后进行concat以获取完整的对象列表。GZip分页JSON响应

如果我尝试JSON.stringify这个,它会失败的大型对象与范围错误。我正在寻找一种方法来处理大型的JSON对象zlib.gzip

回答

0

尝试安装stream-json它将解决您的问题,这是一个很棒的包装流和解析JSON。

//require the modules stream-json 
const StreamArray = require('stream-json/utils/StreamArray'); 
// require fs if your using a file 
const fs = require('fs'); 
const zlib = require('zlib'); 
// Create an instance of StreamArray 
const streamArray = StreamArray.make(); 
fs.createReadStream('./YOUR_FILE.json.gz') 
.pipe(zlib.createUnzip()) // Unzip 
.pipe(streamArray.input); //Read the stream 
//here you can do whatever you want with the stream, 
//you can stream it to response. 
streamArray.output.pipe(process.stdout); 

在示例中,我使用JSON(文件),但可以使用集合并将其传递给流。

希望这是帮助。