Hone logo
Hone
Problems

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:

  1. Encode a given Go struct into a JSON string.
  2. 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 User struct with fields like ID (int), Name (string), Email (string), and IsActive (bool).
  • Implement an EncodeUserToJSON function that takes a User struct and returns its JSON string representation.
  • Implement a DecodeJSONToUser function that takes a JSON string and returns a User struct.
  • Handle potential errors during both encoding and decoding.
  • Ensure that the JSON field names (e.g., is_active instead of IsActive) are handled correctly using struct tags.

Expected Behavior:

  • Encoding a User struct should produce a JSON string with lowercase, snake_case field names.
  • Decoding a JSON string should correctly populate a User struct, 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 User struct 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/json package is encouraged.

Notes

  • The encoding/json package in Go provides functions like json.Marshal for encoding and json.Unmarshal for 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.Marshal and json.Unmarshal.
  • Consider how Go's zero-values are handled when decoding JSON.
Loading editor...
go