Serialize primitives, objects, and arrays to a JSON string
In this challenge, your task is to create a custom implementation of the JSON.stringify() method, which we’ll call myStringify. The JSON.stringify() method in JavaScript is used to convert JavaScript objects or values to a JSON string. However, for the sake of this exercise, you’ll be building your own version to deepen your understanding of how data serialization works in JavaScript. See MDN’s JSON.stringify() reference for the exact output rules of the native method.
Your function should be able to handle the following data types:
Note that unlike the native JSON.stringify(), your function does not need to worry about circular references, functions, or symbols.
Here are some examples to illustrate what your function should do:
Input: ['foo', 'bar']
Output: '["foo", "bar"]'
Input: { name: 'John', age: 30, isEmployed: true }
Output: '{"name": "John", "age": 30, "isEmployed": true}'
Input: 42
Output: '42'
Input: 'Hello, world!'
Output: '"Hello, world!"'
Input: true
Output: 'true'
Input: null
Output: 'null'
Your implementation should correctly convert arrays and objects to their string representations, ensuring to wrap string values in double quotes and separate object properties with commas. Pay close attention to handling edge cases, such as empty objects or arrays.