Serialization Library in Go
This Go package provides a unified interface for serializing and deserializing data across multiple formats including JSON, XML, and YAML. It uses a clean abstraction through an interface to allow interchangeable use of different serialization formats.
Install
go get github.com/uoul/go-serialization
Interface
The core of this library is the ISerializer interface which defines two methods:
Marshal(v any) ([]byte, error) – converts a Go value into serialized bytes
Unmarshal(data []byte, v any) error – parses serialized bytes into a Go value
This interface allows consistent handling of different serialization formats.
Implementations
JSON Serializer
The JsonSerializer struct implements ISerializer using Go's standard encoding/json package. Use NewJsonSerializer() to create an instance.
XML Serializer
The XmlSerializer struct implements ISerializer using Go's standard encoding/xml package. Use NewXmlSerializer() to create an instance.
YAML Serializer
The YamlSerializer struct implements ISerializer using the third-party gopkg.in/yaml.v3 package [^1]. Use NewYamlSerializer() to create an instance.
Usage
Each serializer can be instantiated via its constructor function and used polymorphically through the ISerializer interface:
var serializer ISerializer = NewJsonSerializer()
// or
var serializer ISerializer = NewXmlSerializer()
// or
var serializer ISerializer = NewYamlSerializer()
data, err := serializer.Marshal(myStruct)
if err != nil {
// handle error
}
err = serializer.Unmarshal(data, &myStruct)
if err != nil {
// handle error
}
Dependencies
- YAML support requires
gopkg.in/yaml.v3, which must be included in your go.mod file.