Go JSON Encoding and Decoding Challenge
This challenge focuses on mastering Go's encoding/json package, a fundamental tool for handling JSON data. You'll practice encoding Go data structures into JSON strings and decoding JSON strings back into Go data structures, a common task in web development, API interactions, and data persistence.
Problem Description
Your task is to implement a Go program that can:
- Encode a given Go struct into a JSON string.
- Decode a given JSON string into a Go struct.
You will be provided with a predefined Go struct representing a User and will need to implement functions to perform these encoding and decoding operations.
Key Requirements:
- Define a
Userstruct with fields likeID(int),Name(string),Email(string), andIsActive(bool). - Implement an
EncodeUserToJSONfunction that takes aUserstruct and returns its JSON string representation. - Implement a
DecodeJSONToUserfunction that takes a JSON string and returns aUserstruct. - Handle potential errors during both encoding and decoding.
- Ensure that the JSON field names (e.g.,
is_activeinstead ofIsActive) are handled correctly using struct tags.
Expected Behavior:
- Encoding a
Userstruct should produce a JSON string with lowercase, snake_case field names. - Decoding a JSON string should correctly populate a
Userstruct, mapping snake_case JSON fields to the corresponding Go struct fields.
Edge Cases:
- Consider what happens when decoding invalid JSON.
- Consider what happens when the JSON data is missing a field that exists in the struct.
Examples
Example 1: Encoding a User
Input User:
{
ID: 1,
Name: "Alice Smith",
Email: "alice.smith@example.com",
IsActive: true
}
Output JSON String:
{"id":1,"name":"Alice Smith","email":"alice.smith@example.com","is_active":true}
Explanation: The `User` struct is converted into a JSON string. The `json:"..."` struct tags are used to map the Go field names (e.g., `IsActive`) to the desired JSON field names (e.g., `is_active`).
Example 2: Decoding JSON to a User
Input JSON String:
{"id":2,"name":"Bob Johnson","email":"bob.j@example.com","is_active":false}
Output User:
{
ID: 2,
Name: "Bob Johnson",
Email: "bob.j@example.com",
IsActive: false
}
Explanation: The JSON string is parsed, and its values are used to populate a new `User` struct. The struct tags ensure correct mapping from JSON keys to Go struct fields.
Example 3: Decoding JSON with Missing Field
Input JSON String:
{"id":3,"name":"Charlie Brown","is_active":true}
Output User:
{
ID: 3,
Name: "Charlie Brown",
Email: "", // Default zero-value for string
IsActive: true
}
Explanation: The `Email` field is missing in the JSON. When decoding, the corresponding `Email` field in the `User` struct will retain its zero-value (an empty string).
Constraints
- The
Userstruct will have at least the fields specified in the problem description. - Input JSON strings will be well-formed JSON, though they might be missing some optional fields.
- Performance is not a critical concern for this challenge, but efficient use of the
encoding/jsonpackage is encouraged.
Notes
- The
encoding/jsonpackage in Go provides functions likejson.Marshalfor encoding andjson.Unmarshalfor decoding. - Struct tags (e.g.,
json:"...") are crucial for customizing how Go struct fields are mapped to JSON keys. - Remember to check for and handle errors returned by
json.Marshalandjson.Unmarshal. - Consider how Go's zero-values are handled when decoding JSON.