Hone logo
Hone
Problems

Custom Serialization in Go

Serialization is the process of converting data structures or objects into a format that can be stored or transmitted and later reconstructed. While Go provides the encoding/json, encoding/xml, and encoding/gob packages for common serialization formats, sometimes you need a custom format tailored to specific needs or to optimize for size or performance. This challenge asks you to implement a custom serialization and deserialization process for a simple data structure.

Problem Description

You are tasked with creating a custom serialization format for a Person struct. The Person struct contains a Name (string), Age (int), and a Hobbies slice of strings. Your custom format should represent the data as a string where each field is separated by a semicolon (;). Within each field, values are separated by a comma (,). The order of fields in the serialized string should be: Name, Age, Hobbies. The Hobbies slice should be serialized as a comma-separated list of strings.

You need to implement two functions:

  1. SerializePerson(p Person) string: This function takes a Person struct as input and returns its serialized representation as a string.
  2. DeserializePerson(s string) (Person, error): This function takes a serialized string as input and returns a Person struct. If the input string is invalid or cannot be parsed, it should return an error.

Key Requirements:

  • The serialization format must strictly adhere to the format described above (Name;Age,Hobbies).
  • The DeserializePerson function must handle potential errors during parsing, such as invalid integer values for Age or missing fields.
  • The Hobbies slice should be correctly parsed and populated.
  • Error handling is crucial. Return a meaningful error if deserialization fails.

Expected Behavior:

  • SerializePerson should produce a string in the correct format.
  • DeserializePerson should correctly parse the string and populate the Person struct.
  • DeserializePerson should return an error if the input string is malformed or contains invalid data.

Edge Cases to Consider:

  • Empty Hobbies slice.
  • Invalid integer value for Age (e.g., non-numeric characters).
  • Missing fields in the serialized string.
  • Extra fields in the serialized string.
  • Empty Name string.

Examples

Example 1:

Input: Person{Name: "Alice", Age: 30, Hobbies: []string{"reading", "hiking"}}
Output: "Alice;30,reading,hiking"
Explanation: The Name "Alice" is separated from the Age 30 by a semicolon. The Age 30 is separated from the Hobbies "reading" and "hiking" by a comma.

Example 2:

Input: "Bob;25,coding,music"
Output: Person{Name: "Bob", Age: 25, Hobbies: []string{"coding", "music"}}
Explanation: The string is parsed correctly, and the Person struct is populated with the values.

Example 3:

Input: "Charlie;abc,painting"
Output: error: invalid age: abc
Explanation: The Age value "abc" is not a valid integer, so an error is returned.

Constraints

  • The Name field can contain any valid UTF-8 string.
  • The Age field must be a non-negative integer.
  • The Hobbies slice can contain any number of strings (including zero).
  • The serialized string will always be a single line.
  • The length of the serialized string is expected to be reasonable (less than 1024 characters).

Notes

  • Consider using the strconv package for converting strings to integers.
  • The strings package can be helpful for splitting the serialized string into its components.
  • Pay close attention to error handling to ensure the DeserializePerson function is robust.
  • Think about how to handle edge cases gracefully. Returning a specific error type can be beneficial for debugging.
  • Focus on clarity and readability in your code. Well-structured code is easier to understand and maintain.
Loading editor...
go