Nesting Depth of a JSON Document
Your team's config service stores settings as JSON. Very deeply nested documents have crashed some of its clients, so the service is going to reject any upload nested deeper than a fixed limit. Your job is the measuring part: given one valid JSON document, find its maximum nesting depth.
Every object { ... } and every array [ ... ] adds one level of nesting. The depth of a value is
the number of objects and arrays that contain it, itself included. So [] and {} have depth 1,
[1, {"a": []}] has depth 3, and a document that is just a number, string, true, false or
null has depth 0. Print the largest depth found anywhere in the document.
Brackets and braces that appear inside a string are ordinary text and do not nest anything. Inside a
string, a backslash starts an escape sequence: \" is a quote character that does not end the
string, and \\ is a single backslash character. The other escapes JSON allows are \/, \b,
\f, \n, \r, \t and \uXXXX (four hexadecimal digits).
Input
The whole input is one valid JSON text (as defined by RFC 8259), possibly spread over several lines. It is between 1 and 10^6 bytes long, including line breaks. It consists only of printable ASCII characters (codes 32 to 126), tabs and line breaks. Strings never contain a raw line break or tab: those only appear between tokens, as whitespace.
Output
Print one integer: the maximum nesting depth of the document.
Notes
In the first example the deepest values are the two numbers in [1, 2], which sit inside the outer
object, the "limits" object and the array: depth 3.
In the second example the strings contain brackets, braces, an escaped quote \" and an escaped
backslash \\. None of those count: the string "C:\\temp\\" ends at its last quote, because the
backslash before it is itself escaped. Only the outer array and the object inside it count, so the
depth is 2.
The document can be nested several hundred thousand levels deep, so a recursive parser may run out of stack.