Implementing a Basic JSON.parse in JavaScript
JSON (JavaScript Object Notation) is a ubiquitous data format used for transmitting data in web applications. While JavaScript has a built-in JSON.parse() function, understanding how it works is crucial for grasping data serialization and parsing. This challenge asks you to implement a simplified version of JSON.parse() to parse a JSON string into a JavaScript object.
Problem Description
Your task is to create a JavaScript function called myJSONParse(jsonString) that takes a JSON string as input and returns the corresponding JavaScript object. The function should handle basic JSON data types: strings, numbers, booleans (true, false), null, arrays, and objects. The function should throw an error if the input is not a valid JSON string.
Key Requirements:
- Data Type Handling: Correctly parse strings, numbers, booleans, null, arrays, and objects.
- String Handling: Strings in JSON are enclosed in double quotes. Handle escaped characters within strings (e.g.,
\",\\,\n,\t,\uXXXX). - Array Parsing: Arrays are enclosed in square brackets
[]and contain a comma-separated list of values. - Object Parsing: Objects are enclosed in curly braces
{}and contain a comma-separated list of key-value pairs. Keys must be strings enclosed in double quotes. - Error Handling: Throw an error if the input is not a valid JSON string. A simple error message like "Invalid JSON string" is sufficient.
- Whitespace: Ignore whitespace characters (spaces, tabs, newlines) outside of strings.
Expected Behavior:
The function should return a JavaScript object that accurately represents the structure and data of the input JSON string.
Edge Cases to Consider:
- Empty JSON string:
""should return an empty object{}. - JSON string containing only a single value (e.g.,
"hello",123,true,null). - Nested objects and arrays.
- Invalid JSON syntax (e.g., missing quotes, trailing commas, incorrect brackets).
- Unescaped special characters within strings.
Examples
Example 1:
Input: '{"name": "John Doe", "age": 30, "isStudent": false, "address": null}'
Output: { name: 'John Doe', age: 30, isStudent: false, address: null }
Explanation: The JSON string represents an object with key-value pairs. The function should correctly parse the keys and values into a JavaScript object.
Example 2:
Input: '[1, 2, "hello", true, null]'
Output: [ 1, 2, 'hello', true, null ]
Explanation: The JSON string represents an array. The function should correctly parse the array elements into a JavaScript array.
Example 3:
Input: '{"name": "Jane \\"Doe\\" Doe", "city": "New York\\n"} '
Output: { name: 'Jane "Doe" Doe', city: 'New York\n' }
Explanation: This example tests the handling of escaped characters within strings. The backslashes should be properly interpreted. Whitespace at the end of the string should be ignored.
Example 4:
Input: '{"name": "John",}'
Output: Error: Invalid JSON string
Explanation: Trailing comma is invalid JSON syntax.
Constraints
- The input
jsonStringwill be a string. - The function must handle JSON strings with a maximum length of 1000 characters.
- The function should be reasonably efficient; avoid excessive recursion depth.
- The function should only support the basic JSON data types mentioned above (string, number, boolean, null, array, object). No dates or other complex types are required.
Notes
- This is a simplified implementation of
JSON.parse(). A full implementation would handle more complex scenarios and edge cases. - Consider using a recursive approach to handle nested objects and arrays.
- Pay close attention to the syntax rules of JSON, especially regarding quotes, brackets, and commas.
- Start by parsing the outermost structure (object or array) and then recursively parse the inner elements.
- Regular expressions can be helpful for tokenizing the JSON string, but be careful to avoid overly complex regex patterns.
- Think about how to handle different data types and how to distinguish between them.