Problem, as an interviewer would state it:
"Implement a basic version of JSON.stringify. Discuss edge cases, runtime, and alternatives."
Examples:
jsonStringify({ a: 1, b: undefined, c: [undefined, NaN, new Date(0)] }); // '{"a":1,"c":[null,null,"1970-01-01T00:00:00.000Z"]}'
jsonStringify({ a: [1, { b: 2 }] }, null, 2); // pretty-printed with 2 spaces
jsonStringify(circular); // throws TypeError
Clarifying questions expected:
- How much of the contract:
toJSON, the replacer function and array,space? - What happens to
undefined, functions, symbols,NaN,BigIntand circular structures? - Which keys are included, and in what order?
Code / implementation expected: Yes — real code, checked against the native JSON.stringify on many inputs.