serialize

package
Version: v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package serialize provides unified interface for encoding and decoding data with several implementations (JSON, MsgPack, Gob, ..).

Example usage:

var err error
var byteCoder serialize.ByteCoder
byteCoder = &serialize.JSONCoder{}
byteCoder = &serialize.MsgPackCoder{}

type MyStruct struct {
	A int
	B string
}

dataToSerialize := MyStruct{A: 100, B: "hey"}
encodedBytes, err := byteCoder.Encode(dataToSerialize)
if err != nil {
	fmt.Fprintf(os.Stderr, "serialization failed: %v\n", err)
	os.Exit(1)
}

decodedData := MyStruct{}
err = byteCoder.Decode(encodedBytes, &decodedData)
if err != nil {
	fmt.Fprintf(os.Stderr, "deserialization failed: %v\n", err)
	os.Exit(1)
}

fmt.Printf(
	"Original: %v\nSerialized: %v\nDecoded: %v\n",
	dataToSerialize, encodedBytes, decodedData)

Will print out: Original: {100 hey} Serialized: [130 161 65 100 161 66 163 104 101 121] Decoded: {100 hey}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteCoder

type ByteCoder interface {
	Encode(data interface{}) ([]byte, error)
	Decode(data []byte, vPtr interface{}) error
}

ByteCoder interface for easy switch between serialization formats (JSON, MsgPack, ..)

type GobCoder

type GobCoder struct{}

GobCoder implements ByteCoder interface

func (*GobCoder) Decode

func (e *GobCoder) Decode(data []byte, vPtr interface{}) error

Decode ..

func (*GobCoder) Encode

func (e *GobCoder) Encode(data interface{}) ([]byte, error)

Encode ..

type JSONCoder

type JSONCoder struct{}

JSONCoder implements ByteCoder interface

func (*JSONCoder) Decode

func (e *JSONCoder) Decode(data []byte, vPtr interface{}) (err error)

Decode ..

func (*JSONCoder) Encode

func (e *JSONCoder) Encode(v interface{}) ([]byte, error)

Encode ..

type MsgPackCoder

type MsgPackCoder struct{}

MsgPackCoder implements ByteCoder interface

func (*MsgPackCoder) Decode

func (e *MsgPackCoder) Decode(data []byte, vPtr interface{}) error

Decode ..

func (*MsgPackCoder) Encode

func (e *MsgPackCoder) Encode(v interface{}) ([]byte, error)

Encode ..

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL