Here is the interview question prompt, presented for reference.
In Javascript there is a method called JSON.stringify()
that can convert an object or a variable into a JSON string. The method handles different data types differently, ending up with its correct JSON "translation". Some examples of how this method handles different types include:
Boolean
, Number
, and String
objects are converted to the corresponding primitive values undefined
, Function
, and Symbol
are not valid JSON values and are changed to null
Date
are returned as date.toISOString()
and are treated as stringsInfinity
as well as the value null
are considered as null
Can you implement a simplified version of this JSON stringifier, covering the most important constraints when it comes to the data type and value handling?
Here is an example of how your solution should work:
stringify({}); // '{}'
stringify(true); // 'true'
stringify('foo'); // '"foo"'
stringify([1, 'false', false]); // '[1,"false",false]'
stringify([null, Infinity]); // '[null,null]'
stringify({ x: 5 }); // '{"x":5}'
You can see the full challenge with visuals at this link.
Challenges • Asked over 3 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Implement Json Stringify (Main Thread).