Documenting how JSON parsers of several programming languages deal with deeply nested structures.
Many JSON parsers (and many parsers in general) use recursion to parse nested structures. This is very convenient while programming the parser, but it has consequences on what the parser can parse: indeed, the size of the call stack is usually limited to a value several orders of magnitude smaller than the available RAM, and this implies that a program with too many levels of recursion will fail.
The two most recent JSON standards RFC 8259 and RFC 7159 both say "An implementation may set limits on the maximum depth of nesting". However, the ECMA-404 specification doesn't contain any limit on how deeply nested JSON structures can be.
This means that there is not a defined level of nesting which is correct or incorrect with regard to the JSON specification, and JSON parsers may differ when parsing nested structures.
This repository contains tools to measure the nesting limits of JSON parsers of different languages.
This repository contains a script called test_parser.sh that takes a JSON parser and uses binary search to find the smallest JSON structure it fails to parse and print its nesting level.
The json parser must be a program that reads JSON on its standard input, and exits with a status of 0 if it managed to parse it and any other status if an error occurred.
test_parser.sh constructs json structures composed uniquely of nested arrays, and gives them to the program it tests. For instance, for a depth of 3, it builds the following json : [[[]]]
. This allows to create a structure of only 2n bytes that has n nesting levels.
It uses binary search to find the smallest structure for which the program fails.
On my machine (Ubuntu Linux 4.10.0-35-generic SMP x86_64 with 8Gb RAM, 8.4 MB maximum stack size), I found the following results, sorted from least nesting to most nesting:
language | json library | nesting level | file size | notes |
---|---|---|---|---|
ruby | json | 101 | 202 bytes | |
rust | serde_json | 128 | 256 bytes | |
php | json_decode |
512 | 1024 bytes | maximum depth is configurable |
python3 | json | 994 | 2.0 KB | without sys.setrecursionlimit |
C | jansson | 2049 | 4.0 KB | |
java - gson | Gson | 5670 | 11.3 KB | |
javascript | JSON.parse |
5713 | 11.4 KB | |
java - jackson | Jackson | 6373 | 13 KB | |
C++ | nlohmann::json | 13787 | 27.6 KB | segfault |
Nim | json | 100000 | 200 KB | w/ -d:release |
go | encoding/json |
2581101 | 5.0 MiB | goroutine stack exceeds 1000000000-byte limit |
ruby | Oj | ∞ | ∞ | |
Haskell | Aeson | ∞ | ∞ | available RAM is the only limit |
I tried to test the most popular json library of each language. If you want to add a new language or a new library, feel free to open a pull request.
All the parameters were left to their default values. In particular, the result
for PHP is particular: json_decode
accepts a depth
parameter to configure
the maximum depth of the object to be parsed.