Implement jsonStringify(value), which matches JSON.stringify(value) (no replacer or space arguments) for the cases below. Don't call JSON.stringify.
Primitives:
null,trueandfalseare written asnull,trueandfalse.- Finite numbers are written as
String(n), so-0becomes0and1e21becomes1e+21.NaN,Infinityand-Infinitybecomenull. - Strings are wrapped in double quotes. Escape
"as\"and\as\\. Escape the control characters backspace, form feed, newline, carriage return and tab as\b,\f,\n,\rand\t. Any other character below code 32 becomes\uplus 4 lowercase hex digits, for example\u0001. Everything else is written as it is. - A bigint throws a
TypeError.
Containers are written with no spaces:
- Arrays:
[item,item]. - Objects:
{"key":value,...}for each own key fromObject.keys, in that order. Keys are quoted and escaped like strings. - If a value has a
toJSONmethod (dates do), write whattoJSON()returns instead.
Values JSON can't hold (undefined, functions, symbols): at the top level, return undefined. As an object property, leave the property out. As an array item, write null.
Cycles: if an object contains itself, directly or deeper down, throw a TypeError. The same object appearing twice without a cycle is fine.
Input: jsonStringify({ id: 7, tags: ['a'], at: undefined })
Output: '{"id":7,"tags":["a"]}'
Input: jsonStringify([NaN, () => 1, 'say "hi"'])
Output: '[null,null,"say \\"hi\\""]'
Input: jsonStringify(undefined)
Output: undefined