JSON Encoding and Decoding in Go
This challenge focuses on implementing JSON encoding and decoding functionality in Go. JSON (JavaScript Object Notation) is a widely used data format for transmitting data between applications, and proficiency in handling it is crucial for many Go developers. You will be tasked with converting Go data structures to JSON and vice versa.
Problem Description
You are required to write a Go program that can encode a given Go data structure (a struct) into a JSON string and decode a JSON string back into the same Go data structure. The program should handle basic data types (string, int, float64, bool) within the struct and correctly represent them in JSON format. Error handling is a critical component; your program must gracefully handle potential errors during both encoding and decoding.
What needs to be achieved:
- Define a Go struct representing a simple data object (e.g., a person with name, age, and isEmployed).
- Implement a function
EncodeToJSONthat takes an instance of this struct as input and returns a JSON string representation of the struct. - Implement a function
DecodeFromJSONthat takes a JSON string as input and returns an instance of the struct. This function should also return an error if the decoding fails.
Key Requirements:
- The
EncodeToJSONfunction must use theencoding/jsonpackage to convert the struct to JSON. - The
DecodeFromJSONfunction must use theencoding/jsonpackage to convert the JSON string back to the struct. - Both functions must handle potential errors appropriately and return them.
- The struct fields should be exported (begin with a capital letter) for JSON encoding/decoding to work correctly.
Expected Behavior:
- The
EncodeToJSONfunction should return a valid JSON string representing the struct's data. - The
DecodeFromJSONfunction should successfully populate the struct with data from the JSON string if the string is valid. - If either function encounters an error (e.g., invalid JSON format, type mismatch), it should return an appropriate error message.
Edge Cases to Consider:
- Empty JSON string.
- Invalid JSON string (e.g., missing quotes, incorrect syntax).
- JSON string with fields not present in the struct.
- JSON string with fields of incorrect types compared to the struct.
- Nil values for struct fields.
Examples
Example 1:
Input:
struct: { Name: "Alice", Age: 30, IsEmployed: true }
Output:
"{\"Name\":\"Alice\",\"Age\":30,\"IsEmployed\":true}"
Explanation: The struct is encoded into a JSON string with the correct key-value pairs.
Example 2:
Input:
JSON String: "{\"Name\":\"Bob\",\"Age\":25,\"IsEmployed\":false}"
Output:
struct: { Name: "Bob", Age: 25, IsEmployed: false }
Explanation: The JSON string is decoded back into a struct with the corresponding values.
Example 3: (Edge Case)
Input:
JSON String: "{\"Name\":\"Charlie\",\"Age\":\"thirty\"}"
Output:
Error: "invalid type for value "thirty": string, expected int64"
Explanation: The JSON string contains an invalid age value (string instead of integer), resulting in a decoding error.
Constraints
- The struct will contain only string, int, float64, and bool fields.
- The JSON string will be a valid JSON string (though it may contain errors during decoding).
- The program should be efficient enough to handle reasonably sized structs and JSON strings (up to 1MB).
- Error messages should be informative and helpful for debugging.
Notes
- The
encoding/jsonpackage in Go provides built-in support for JSON encoding and decoding. Utilize its functions likejson.Marshalfor encoding andjson.Unmarshalfor decoding. - Consider using the
omitemptytag in the struct definition to exclude fields with zero values from the JSON output. For example:Name string \json:"name,omitempty"`` - Pay close attention to error handling. Always check the error returned by
json.Unmarshaland handle it appropriately. - Think about how to handle cases where the JSON string contains fields that are not present in the struct. You might choose to ignore them or return an error.