对于数组深度,我指的不是数组中的元素数量,而是这里的构造:
[[[[[[[[[[[[[[[[[[[["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
, ornull
.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/