Hone logo
Hone
Problems

Implement JSON.stringify in JavaScript

This challenge asks you to build a custom implementation of JavaScript's built-in JSON.stringify function. This is a fundamental operation for serializing JavaScript objects into JSON strings, essential for data transmission and storage. Understanding how it works under the hood will deepen your grasp of JavaScript's data types and object manipulation.

Problem Description

Your task is to create a JavaScript function named myStringify that takes a JavaScript value as input and returns its JSON string representation. You should handle common JavaScript data types and their corresponding JSON equivalents.

Key Requirements:

  • Handle Primitive Types:
    • Strings: Should be enclosed in double quotes. Special characters like " and \ should be escaped.
    • Numbers: Should be represented as their numeric literals. Infinity, -Infinity, and NaN should be serialized as null.
    • Booleans: true should be "true", false should be "false".
    • null: Should be "null".
  • Handle Complex Types:
    • Arrays: Should be represented as JSON arrays [...]. Each element should be stringified recursively.
    • Objects: Should be represented as JSON objects {...}. Keys should be stringified and enclosed in double quotes. Values should be stringified recursively.
    • undefined: When encountered as a value in an object, it should be omitted. When encountered as an element in an array, it should be serialized as null.
    • Functions: Should be omitted from objects and serialized as null in arrays.
    • Symbols: Should be omitted from objects and serialized as null in arrays.
  • Recursive Stringification: The function must correctly handle nested objects and arrays.

Expected Behavior:

The output should be a valid JSON string.

Edge Cases to Consider:

  • Circular references in objects (though a full implementation is complex, for this challenge, you can assume no circular references or implement a basic detection/handling).
  • Empty objects and arrays.
  • Objects with null or undefined values.
  • Arrays with undefined, null, or function values.

Examples

Example 1:

Input: { "name": "Alice", "age": 30, "isStudent": false, "city": null }
Output: {"name":"Alice","age":30,"isStudent":false,"city":null}
Explanation: All values are directly translatable to JSON. Keys are quoted.

Example 2:

Input: [1, "hello", true, null, [2, "world"]]
Output: [1,"hello",true,null,[2,"world"]]
Explanation: Array elements are stringified recursively.

Example 3:

Input: { "data": { "items": [1, undefined, 3], "config": { "enabled": true, "callback": function() {} } }, "version": NaN }
Output: {"data":{"items":[1,null,3],"config":{"enabled":true}},"version":null}
Explanation: `undefined` in an array becomes `null`. Functions are omitted from objects. `NaN` becomes `null`.

Example 4:

Input: { "a": undefined, "b": function() {}, "c": Symbol("key") }
Output: {}
Explanation: `undefined`, functions, and Symbols are omitted as object properties.

Constraints

  • The input will be a valid JavaScript value.
  • Assume no circular references for a basic implementation. If you want to add this, clearly state your approach.
  • Performance is not a primary concern for this challenge, but the solution should be reasonably efficient.
  • Do not use JSON.parse or JSON.stringify within your myStringify implementation.

Notes

  • Remember to handle string escaping correctly for characters like " and \.
  • Consider how you will iterate over object keys and array elements.
  • For objects, only enumerable own properties should be considered.
  • This challenge focuses on the core serialization logic. A full JSON.stringify has many more options (like replacer and space), which are out of scope for this problem.
Loading editor...
javascript