Reimplementing JavaScript's JSON.parse
This challenge asks you to build a simplified version of JavaScript's built-in JSON.parse function. Understanding how JSON parsing works is fundamental to handling data exchange in web applications, and reimplementing it provides deep insight into string manipulation, state management, and recursive parsing techniques.
Problem Description
Your task is to create a JavaScript function myJsonParse(jsonString) that takes a string representing a JSON value and returns the corresponding JavaScript value. You should aim to support a subset of valid JSON, focusing on the core data types.
Key Requirements:
- Support for Primitive Types:
string: Enclosed in double quotes ("), with support for basic escape sequences like\",\\,\/,\b,\f,\n,\r,\t, and\uXXXX(where XXXX is a four-digit hexadecimal number).number: Integers and floating-point numbers (including scientific notation).boolean:trueandfalse.null: The literalnull.
- Support for Composite Types:
array: Ordered lists of JSON values, enclosed in square brackets ([]), with elements separated by commas (,).object: Unordered collections of key-value pairs, enclosed in curly braces ({}), with keys as strings and values as any valid JSON value. Key-value pairs are separated by commas (,), and the key is separated from the value by a colon (:).
- Whitespace Handling: Ignore whitespace (spaces, tabs, newlines, carriage returns) around tokens (like commas, colons, brackets, braces, and between values).
- Error Handling: For simplicity, this challenge does not require strict error handling for malformed JSON. Assume the input JSON string is syntactically valid within the supported subset.
Expected Behavior:
The function should return the equivalent JavaScript data type. For example:
"hello"should become"hello"123should become123trueshould becometruenullshould becomenull[1, "a", true]should become[1, "a", true]{"name": "Alice", "age": 30}should become{ name: "Alice", age: 30 }
Edge Cases to Consider:
- Empty arrays (
[]) and empty objects ({}). - Nested arrays and objects.
- Strings containing escaped characters.
- Numbers with decimal points and exponents.
- Whitespace variations.
Examples
Example 1:
Input: '"Hello, World!"'
Output: "Hello, World!"
Explanation: Parses a simple JSON string literal.
Example 2:
Input: '123.45e-2'
Output: 1.2345
Explanation: Parses a JSON number with a decimal and exponent.
Example 3:
Input: '[true, false, null]'
Output: [true, false, null]
Explanation: Parses a JSON array containing boolean and null values.
Example 4:
Input: '{"name": "Bob", "isActive": true, "hobbies": ["reading", "coding"]}'
Output: { name: "Bob", isActive: true, hobbies: ["reading", "coding"] }
Explanation: Parses a nested JSON object with various data types.
Example 5:
Input: '"This string has an escaped quote: \\""'
Output: 'This string has an escaped quote: "'
Explanation: Demonstrates parsing of escaped double quotes within a string.
Constraints
- The input
jsonStringwill be a string. - The input will conform to a subset of the JSON specification, as described in the "Key Requirements". You do not need to implement support for all JSON features (e.g., numbers with leading zeros, octal/hexadecimal representations, or complex unicode escapes beyond
\uXXXX). - Performance is not a primary concern for this challenge, but your solution should not be excessively inefficient (e.g., exponential time complexity for simple cases).
Notes
This is a challenging problem that often involves a recursive descent parser. You'll need to manage the current position within the input string as you parse different types of JSON values. Consider creating helper functions for parsing specific types (like strings, numbers, arrays, and objects) to keep your code organized. Regular expressions can be useful for tokenizing or validating certain parts of the JSON, but the core parsing logic will likely require more manual string traversal. Remember to handle whitespace effectively by skipping it when necessary.