Practice/Meta/Leetcode 2633. Convert Object to JSON String
CodingOptional
Write a function that converts a data structure into a valid JSON-formatted string. Your serializer must handle primitive types (numbers, strings, booleans, null), arrays, and objects (dictionaries/maps). The output string must conform to JSON standards, including proper escaping of special characters within strings.
You cannot use built-in JSON serialization functions like JSON.stringify() or json.dumps(). The goal is to implement the serialization logic from scratch.
"), backslashes (\), and control characters (newline, tab, etc.)Example 1:
Input: \{"name": "Bob", "age": 25\} Output: "\{\"name\":\"Bob\",\"age\":25\}" Explanation: Object with string and number values serialized with proper formatting
Example 2:
Input: [1, 2, [3, 4]] Output: "[1,2,[3,4]]" Explanation: Nested array structure with proper bracket nesting
Example 3:
Input: \{"text": "Quote: \"Hello\""\} Output: "\{\"text\":\"Quote: \\\"Hello\\\"\"\}" Explanation: Double quotes within string values must be escaped with backslashes