使用在两个(或更多)缓冲区块之间拆分的大型 JSON 的最佳方法是什么
What is the best way to work with a large JSON splitted between two (or more) buffer chunks
想象一下你有一个很大的 JSON 基本上看起来像这样的情况:
{"name": "...", /*...*/, "data": "..."}
那个JSON其实很大。此外,JSON 在 node.js 缓冲区块中传递给我。因为 JSON 很大,所以有两个块,第一个包含 name
,第二个包含 data
。
块可以如下所示:
{"name": "...", /*...*/, "da //chunk1
ta": "..."} //chunk2
我需要在name
的基础上修改data
。这样做的最佳做法是什么?
我遇到的问题是由于明显的原因(包括内存消耗)我无法 JSON.parse
此数据。那么在没有 JSON.parse
的情况下如何处理这种情况?我是否必须编写自己的解析器来检测 data
的开始和结束位置?
您的选择相当有限:
Concatenating the buffers,解析 JSON,然后使用生成的对象树(内存影响和所有)。
使用(或构建)流式 JSON 解析器。例如,Oboe.js(无从属关系)被描述为:
Oboe.js is an open source Javascript library for loading JSON using streaming, combining the convenience of DOM with the speed and fluidity of SAX.
It can parse any JSON as a stream, is small enough to be a micro-library, doesn't have dependencies, and doesn't care which other libraries you need it to speak to.
We can load trees larger than the available memory. Or we can instantiate classical OOP models from JSON, or completely transform your JSON while it is being read.
我发现使用搜索 "node streaming json parser"; this question 是第二个命中,并且有一个参考双簧管的答案。
想象一下你有一个很大的 JSON 基本上看起来像这样的情况:
{"name": "...", /*...*/, "data": "..."}
那个JSON其实很大。此外,JSON 在 node.js 缓冲区块中传递给我。因为 JSON 很大,所以有两个块,第一个包含 name
,第二个包含 data
。
块可以如下所示:
{"name": "...", /*...*/, "da //chunk1
ta": "..."} //chunk2
我需要在name
的基础上修改data
。这样做的最佳做法是什么?
我遇到的问题是由于明显的原因(包括内存消耗)我无法 JSON.parse
此数据。那么在没有 JSON.parse
的情况下如何处理这种情况?我是否必须编写自己的解析器来检测 data
的开始和结束位置?
您的选择相当有限:
Concatenating the buffers,解析 JSON,然后使用生成的对象树(内存影响和所有)。
使用(或构建)流式 JSON 解析器。例如,Oboe.js(无从属关系)被描述为:
Oboe.js is an open source Javascript library for loading JSON using streaming, combining the convenience of DOM with the speed and fluidity of SAX.
It can parse any JSON as a stream, is small enough to be a micro-library, doesn't have dependencies, and doesn't care which other libraries you need it to speak to.
We can load trees larger than the available memory. Or we can instantiate classical OOP models from JSON, or completely transform your JSON while it is being read.
我发现使用搜索 "node streaming json parser"; this question 是第二个命中,并且有一个参考双簧管的答案。