Implement JSON.stringify
Write jsonStringify(value) producing the same output as JSON.stringify(value) with no indentation, for these inputs: null, booleans, numbers, strings, arrays, and plain objects, nested.
- Non-finite numbers (
NaN,Infinity) serialize asnull. - In an array,
undefinedand functions becomenull; in an object, keys with those values are omitted. - Strings are wrapped in double quotes with
",\, and control characters escaped. - A top-level
undefinedor function returnsundefined.
jsonStringify({ a: 1, b: [true, null, undefined], c: undefined }) // '{"a":1,"b":[true,null,null]}'
jsonStringify("say \"hi\"") // '"say \\"hi\\""'Hints
Hint 1
Dispatch on type: null and finite numbers and booleans print as String(value); strings get quoted and escaped; arrays and objects recurse.
Hint 2
Objects need the key quoted too, and a value that serializes to undefined means skip the key.