serialization

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 3 Imported by: 1

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ISerializer

type ISerializer interface {
	ContentType() string
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, v any) error
}

func NewJSONSerializer

func NewJSONSerializer() ISerializer

func NewXmlSerializer

func NewXmlSerializer() ISerializer

func NewYamlSerializer

func NewYamlSerializer() ISerializer

type JsonSerializer

type JsonSerializer struct{}

func (*JsonSerializer) ContentType added in v1.0.1

func (j *JsonSerializer) ContentType() string

ContentType implements ISerializer.

func (*JsonSerializer) Marshal

func (j *JsonSerializer) Marshal(v any) ([]byte, error)

Marshal implements ISerializer.

func (*JsonSerializer) Unmarshal

func (j *JsonSerializer) Unmarshal(data []byte, v any) error

Unmarshal implements ISerializer.

type XmlSerializer

type XmlSerializer struct{}

func (*XmlSerializer) ContentType added in v1.0.1

func (j *XmlSerializer) ContentType() string

ContentType implements ISerializer.

func (*XmlSerializer) Marshal

func (j *XmlSerializer) Marshal(v any) ([]byte, error)

Marshal implements ISerializer.

func (*XmlSerializer) Unmarshal

func (j *XmlSerializer) Unmarshal(data []byte, v any) error

Unmarshal implements ISerializer.

type YamlSerializer

type YamlSerializer struct{}

func (*YamlSerializer) ContentType added in v1.0.1

func (j *YamlSerializer) ContentType() string

ContentType implements ISerializer.

func (*YamlSerializer) Marshal

func (j *YamlSerializer) Marshal(v any) ([]byte, error)

Marshal implements ISerializer.

func (*YamlSerializer) Unmarshal

func (j *YamlSerializer) Unmarshal(data []byte, v any) error

Unmarshal implements ISerializer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL