Problem, as an interviewer would state it:
"Implement a secure polyfill for JSON.parse using a recursive descent parsing approach, validating characters to prevent arbitrary script execution. Discuss edge cases, runtime, and alternatives."
Examples:
jsonParse('{"a":[1,2.5,-3e2,true,null],"b":{"c":"x\\ny"}}'); // { a: [1, 2.5, -300, true, null], b: { c: "x\ny" } }
jsonParse('{"a":1,}'); // throws SyntaxError (trailing comma)
jsonParse('{"n":"2026-01-15T10:30:00.000Z"}', (k, v) => (k === "n" ? new Date(v) : v)); // reviver support
Clarifying questions expected:
- How strict must it be (trailing commas, single quotes, comments, leading zeros,
NaN)? - Which error should it throw, and must the optional reviver be supported?
- How should
__proto__keys, duplicate keys and very deep nesting behave?
Code / implementation expected: Yes — a real parser, checked against the native JSON.parse on many inputs.