Hone logo
Hone
Problems

JSON Unmarshaling in Go: Building a Simple Data Parser

JSON (JavaScript Object Notation) is a ubiquitous data format used for data interchange. This challenge asks you to implement a simplified JSON unmarshaler in Go, focusing on parsing JSON objects containing strings, numbers (integers and floats), booleans, and null values into corresponding Go data structures. This exercise will solidify your understanding of Go's data types, error handling, and string manipulation.

Problem Description

You are tasked with creating a function UnmarshalJSON that takes a JSON string as input and parses it into a Go map[string]interface{}. The map will store the parsed JSON data, where keys are strings (representing JSON object keys) and values are the corresponding Go values. The function should handle the following JSON data types:

  • String: Parsed into a string.
  • Number (Integer): Parsed into an int.
  • Number (Float): Parsed into a float64.
  • Boolean: Parsed into a bool.
  • Null: Parsed into nil.

The function should return the parsed map[string]interface{} and an error if the input is not valid JSON or if any parsing error occurs. The JSON string will represent a single JSON object (enclosed in curly braces {}).

Key Requirements:

  • The function must correctly parse the JSON string into a map[string]interface{}.
  • The function must handle different JSON data types as described above.
  • The function must return an error if the input is not a valid JSON object or if parsing fails.
  • The function should be robust and handle potential errors gracefully.

Expected Behavior:

The function should parse the JSON string and populate the map with the corresponding Go values. If a key is not found in the JSON string, it should not be added to the map. If the JSON string is empty or not a valid JSON object, the function should return an error.

Edge Cases to Consider:

  • Empty JSON string ("").
  • Invalid JSON string (e.g., missing quotes, incorrect syntax).
  • JSON string containing only a single key-value pair.
  • JSON string with nested objects (this challenge does not require handling nested objects - treat them as errors).
  • JSON string with multiple key-value pairs.
  • JSON string with duplicate keys (the last value encountered for a key should be used).

Examples

Example 1:

Input: "{\"name\": \"Alice\", \"age\": 30, \"isStudent\": false, \"address\": null}"
Output: map[string]interface{}{"name": "Alice", "age": 30, "isStudent": false, "address": nil}
Explanation: The JSON string is parsed into a map with string keys and interface{} values.  Each key-value pair is correctly mapped to the corresponding Go type.

Example 2:

Input: "{\"city\": \"New York\", \"temperature\": 25.5}"
Output: map[string]interface{}{"city": "New York", "temperature": 25.5}
Explanation:  The JSON string is parsed, and the "city" string and "temperature" float64 are correctly stored in the map.

Example 3:

Input: "{}"
Output: map[string]interface{}{}
Explanation: An empty JSON object results in an empty map.

Example 4:

Input: "invalid json"
Output: error: "invalid JSON format"
Explanation: An invalid JSON string results in an error.

Constraints

  • The input JSON string will be a single JSON object (enclosed in curly braces {}).
  • The JSON string will only contain strings, integers, floats, booleans, and null values.
  • Nested JSON objects are not supported and should result in an error.
  • The function must be implemented in Go.
  • The function should handle errors gracefully and return meaningful error messages.

Notes

  • You can use Go's built-in strconv package for converting strings to numbers.
  • Consider using a state machine or recursive approach to parse the JSON string. However, for this simplified version, a more iterative approach is acceptable.
  • Focus on correctly parsing the JSON string and mapping it to the map[string]interface{}. Error handling is crucial.
  • Remember that interface{} can hold any type in Go, so you'll need to handle type assertions when accessing values from the map.
  • This is a simplified unmarshaler; a production-ready unmarshaler would handle more complex JSON structures and data types.
Loading editor...
go