call(back)
FrontendmediumSign in to star problems

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 as null.
  • In an array, undefined and functions become null; in an object, keys with those values are omitted.
  • Strings are wrapped in double quotes with ", \, and control characters escaped.
  • A top-level undefined or function returns undefined.
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.

Asked at