Hone logo
Hone
Problems

Implementing a Basic JSON.stringify in JavaScript

The built-in JSON.stringify() method in JavaScript is essential for converting JavaScript objects and arrays into JSON strings. This challenge asks you to implement a simplified version of this functionality, focusing on core data types and structures. Understanding how JSON.stringify() works under the hood is a valuable exercise in understanding JavaScript's data representation and string manipulation.

Problem Description

Your task is to create a function called stringify(obj) that mimics the behavior of JSON.stringify() for a subset of JavaScript data types. The function should take a JavaScript object or array as input and return a JSON string representation of that input.

Key Requirements:

  • Supported Types: The function must correctly handle the following JavaScript data types:
    • Strings (enclosed in double quotes)
    • Numbers
    • Booleans (true and false)
    • null
    • Arrays (represented as JSON arrays)
    • Objects (represented as JSON objects)
  • Nested Structures: The function must correctly handle nested objects and arrays.
  • No Functions or Symbols: The function should not attempt to serialize functions or Symbols. If it encounters a function or symbol, it should return undefined for that property.
  • Order of Properties: The order of properties in objects does not need to be preserved.
  • Whitespace: No whitespace is required in the output string.

Expected Behavior:

The function should return a valid JSON string. Invalid JSON strings (e.g., missing quotes, incorrect delimiters) should not be produced.

Edge Cases to Consider:

  • Empty objects and arrays.
  • Objects with null or undefined values.
  • Arrays containing a mix of data types.
  • Circular references (your implementation does not need to handle circular references; they can be assumed not to exist in the input).

Examples

Example 1:

Input: { "name": "John", "age": 30, "city": "New York" }
Output: '{"name":"John","age":30,"city":"New York"}'
Explanation: The object's properties are converted to key-value pairs in a JSON format.

Example 2:

Input: [1, "hello", true, null]
Output: '[1,"hello",true,null]'
Explanation: The array's elements are converted to a JSON array.

Example 3:

Input: { "a": 1, "b": { "c": 2, "d": "test" }, "e": [3, 4] }
Output: '{"a":1,"b":{"c":2,"d":"test"},"e":[3,4]}'
Explanation: Demonstrates handling nested objects and arrays.

Example 4:

Input: { "name": "Alice", "greet": function() { console.log("Hi!"); }, "symbol": Symbol("test") }
Output: '{"name":"Alice","symbol":undefined}'
Explanation: Functions and Symbols are ignored and represented as undefined.

Constraints

  • The input obj will be a JavaScript object or array.
  • The input will not contain circular references.
  • The output string should be a valid JSON string.
  • The function should not throw errors.
  • Performance is not a primary concern for this exercise, but avoid excessively inefficient algorithms.

Notes

  • Consider using recursion to handle nested objects and arrays.
  • Pay close attention to the correct JSON syntax for strings, numbers, booleans, and null values.
  • Think about how to handle different data types appropriately.
  • Start with simple cases and gradually increase the complexity of your test inputs.
  • Remember that JSON strings require double quotes for strings, not single quotes.
Loading editor...
javascript