English 中文(简体)
不能在JSON制造超过0x1fffe8的体质?
原标题:Cannot create a string longer than 0x1fffffe8 characters in JSON.parse?

我有JSON文档,其大小数据为914MB。 我将文件装上fs-extra并加上。 但是,如果一行,就会产生错误。

不能造成超过0x1fffe8级的窒息性。

下面是法典

        const fs = require( fs-extra );
        const rawdata = fs.readFileSync( src/raw.json );
        const data = JSON.parse(rawdata);

我正在以<代码>npm开展这一项目,并运行在<编码> 包装.json以下。

"scripts": {
   
    "start:dev": "cross-env NODE_OPTIONS= --max-old-space-size=4096  ts-node -r tsconfig-paths/register ./src --env=development",
  
  }
问题回答

0x1fffffe8 is exactly 512MB.

很多评论家是正确的:你正在放弃制度限制。 我同意“点”的说法,即它大多数可能有一个长限:fs-extra与限额无关。

无论如何,你将不得不处理猪肉。 以下是这样做的不同方式。

A: Use a SAX-style JSON parser

你有许多教区选择。 为了让大家开始,我发现在国家预防机制上有一对:

B: Implement a Node Streams pipeline

Almost certainly your massive JSON data is a array at the root level. This approach uses a parser that can asynchronously process each element in that array individually, or in batches, whichever makes sense. It is based on the very powerful and flexible Node Streams API.

i) 如果你的数据为JSON阵列,而是一种被压缩的JSON物体的流体,那么它可能符合。 见以下备选办法:<代码>D。

C: Manual chunking

? 如果你的数据支持,这很可能是最有效的。

这一选择与<条码>B一样,但不是使用一种流层子,而是使用king。 如果初专干事数据阵列的要素非常固定,例如每个元件都完全按N行。 你们可以很容易地在没有教条的情况下提取。

例如,如果你的数据表明:

{
  data: [
    { name: ...,
      address: ... },
    { name: ...,
      address: ... },
    { name: ...,
      address: ... },
    { name: ...,
      address: ... }
  ]
}

你们的工作就是这样:

  1. Use a buffered reader to read the file. (DO NOT synchronously read it all into memory)
  2. Discard the first two lines
  3. Read the file in chunks, two lines at a time
  4. If a chunk starts with {, remove any trailing comma and parse each individual {name:..., address:...} record.
  5. If it doesn t, you have reached the end of the array. Discard the rest of the file or hand it off to some other process if you expect some other data there.

The details will depend on your data.

D: Use a JSON Streaming protocol parser

JSON 简化程序是一流中聚集的多目标。 如果是你的话,你就应当使用支持该议定书的教官。

The V8 string size limit

import NodeBuffer from "node:buffer";

NodeBuffer.constants.MAX_STRING_LENGTH

Represents the largest length that a string primitive can have, counted in UTF-16 code units.

这一数值可能取决于正在使用的联合材料发动机。

对<条码>载施加的限制并不涉及星数。 它的0-x1FFFE8(229-24)UTF-16单位,载于我的Node.js 20.1.0。


Node.js使用V8发动机。 A Javascript string composed of 2-octet (uint16_t) UTF-16代码单位,以及在V8发动机中,这种装置的最大数量(string.length) 是

  • (2²⁸-16) (0x0FFFFFF0 UTF-16 code units / about 0.5 GiB) on a 32-bit system
  • and (2²⁹-24) (0x1FFFFFE8 UTF-16 code units / about 1 GiB) on a 64-bit system.

node/lib/buffer.js

const constants = ObjectDefineProperties({}, {
  MAX_LENGTH: {
    __proto__: null,
    value: kMaxLength,
    writable: false,
    enumerable: true,
  },
  MAX_STRING_LENGTH: {
    __proto__: null,
    value: kStringMaxLength,
    writable: false,
    enumerable: true,
  },
});

node/src/node_buffer.cc

  target
      ->Set(context,
            FIXED_ONE_BYTE_STRING(isolate, "kStringMaxLength"),
            Integer::New(isolate, String::kMaxLength))
      .Check();

v8/include/v8-primitive.h

/**
 * A JavaScript string value (ECMA-262, 4.3.17).
 */
class V8_EXPORT String : public Name {
 public:
  static constexpr int kMaxLength =
      internal::kApiSystemPointerSize == 4 ? (1 << 28) - 16 : (1 << 29) - 24;

v8/include/v8-internal.h

/**
 * Configuration of tagging scheme.
 */
const int kApiSystemPointerSize = sizeof(void*);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签