arrays - JSON 中有数组深度限制吗?

标签 arrays json

对于数组深度,我指的不是数组中的元素数量,而是这里的构造:

[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]

我在 test suite 中偶然发现了这一点对于 JSON_checker .

脚本pass2.json有这个(19个左括号):

[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]

正如测试脚本的名称所暗示的那样,预计会通过。

另一个来自 fail18.json,预计会失败。它有 20 个开口支架。然而,ECMA-404关于数组没有那么多可说:

An array structure is a pair of square bracket tokens surrounding zero or more values. The values are separated by commas. The order of the values is significant.

是否有我错过的限制或者测试脚本是否错误?

最佳答案

正如您已经指出的,ECMA-404 JSON Data Interchange Format没有提到任何嵌套级别的限制,只是数组/对象可以嵌套:

Because objects and arrays can nest, trees and other complex data structures can be represented.

A JSON value can be an object, array, number, string, true, false, or null.

An array structure is a pair of square bracket tokens surrounding zero or more values.

您提到的测试套件中的限制似乎更多是对程序的内存使用设置限制的措施。请注意 JSON_checker.c 中函数 new_JSON_checker 的定义,它也是可用的源文件之一 on github 。注释中解释了深度参数的使用:

It takes a depth parameter that restricts the level of maximum nesting.

然后代码使用此参数进行内存分配:

jc->stack = (int*)calloc(depth, sizeof(int));

范围检查是在其他地方进行的,注释如下:

Return false if there is overflow.

因此,总而言之,这似乎更像是针对此特定测试套件的实用措施。

很明显,其他解析器接受更大值的深度:例如尝试 jsonlint.com .

关于arrays - JSON 中有数组深度限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42116718/

相关文章:

c - 使用模运算符 c 遍历数组

python - 使用python从ustream api解码json

ruby-on-rails - 将大 json 文件存储在数据库中的最佳方式(postgres 或 mongo)

json - 在子文档中推送 JSON 文档仅插入 ObjectId

c++ - 声明可变大小数组时出现意外的编译器行为

c - 是否可以在 C 中将 char** 转换为 char*?

javascript - 如何按业力对 Reddit 用户进行排序?

objective-c - 替换数组中的对象不起作用,但添加一个新对象可以

Ajax JSON 转为 Highcharts 饼图

c# - Newtonsoft Json 序列化程序无法使用继承自 List 的自定义类