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:
SerializePerson(p Person) string: This function takes aPersonstruct as input and returns its serialized representation as a string.DeserializePerson(s string) (Person, error): This function takes a serialized string as input and returns aPersonstruct. 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
DeserializePersonfunction must handle potential errors during parsing, such as invalid integer values for Age or missing fields. - The
Hobbiesslice should be correctly parsed and populated. - Error handling is crucial. Return a meaningful error if deserialization fails.
Expected Behavior:
SerializePersonshould produce a string in the correct format.DeserializePersonshould correctly parse the string and populate thePersonstruct.DeserializePersonshould return an error if the input string is malformed or contains invalid data.
Edge Cases to Consider:
- Empty
Hobbiesslice. - 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
Namefield can contain any valid UTF-8 string. - The
Agefield must be a non-negative integer. - The
Hobbiesslice 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
strconvpackage for converting strings to integers. - The
stringspackage can be helpful for splitting the serialized string into its components. - Pay close attention to error handling to ensure the
DeserializePersonfunction 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.