Hone logo
Hone
Problems

Go JSON Marshaling: Employee Data Serialization

This challenge focuses on serializing Go data structures into JSON format, a fundamental task in modern application development for data exchange and storage. You will implement a function that takes a Go struct representing employee data and converts it into a JSON string. This skill is crucial for building APIs, configuring applications, and interacting with various web services.

Problem Description

Your task is to create a Go function named MarshalEmployee that takes a pointer to an Employee struct and returns its JSON representation as a byte slice. The Employee struct contains fields for ID, Name, Department, and IsFullTime.

Key Requirements:

  • Define a Go struct named Employee with the following fields:
    • ID: an integer representing the employee's unique identifier.
    • Name: a string representing the employee's full name.
    • Department: a string representing the employee's department.
    • IsFullTime: a boolean indicating whether the employee is full-time.
  • Implement the MarshalEmployee function:
    • It should accept *Employee as its input parameter.
    • It should return a []byte containing the JSON representation of the input Employee struct.
    • The JSON keys should correspond to the struct field names, but with the first letter capitalized (e.g., ID becomes "ID", Name becomes "Name").
    • If the input Employee pointer is nil, the function should return an empty byte slice ([]byte{}).
  • You should leverage Go's built-in encoding/json package for this task.

Expected Behavior:

The function should correctly serialize the provided Employee data into a valid JSON string.

Edge Cases to Consider:

  • Handling a nil pointer for the Employee struct.

Examples

Example 1:

Input: &Employee{ID: 101, Name: "Alice Smith", Department: "Engineering", IsFullTime: true}
Output: []byte(`{"ID":101,"Name":"Alice Smith","Department":"Engineering","IsFullTime":true}`)
Explanation: The Employee struct is serialized into a JSON byte slice. The keys in the JSON match the capitalized struct field names.

Example 2:

Input: &Employee{ID: 205, Name: "Bob Johnson", Department: "Marketing", IsFullTime: false}
Output: []byte(`{"ID":205,"Name":"Bob Johnson","Department":"Marketing","IsFullTime":false}`)
Explanation: Another example demonstrating the serialization of a different employee.

Example 3:

Input: nil
Output: []byte{}
Explanation: When a nil Employee pointer is provided, an empty byte slice is returned.

Constraints

  • The Employee struct will not contain nested complex data structures (e.g., slices of structs, maps of slices) for this specific challenge.
  • The number of fields in the Employee struct will not exceed 10.
  • The length of strings (Name, Department) will not exceed 255 characters.
  • Performance is not a critical concern for this basic marshaling task.

Notes

  • Go's encoding/json package provides a Marshal function that is perfect for this task.
  • Pay attention to the exact naming of the JSON keys. The default behavior of json.Marshal should align with the requirement if you define your struct fields correctly.
  • Consider how to handle the nil input case explicitly within your function.
Loading editor...
go