tinyserializer

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: GPL-2.0 Imports: 7 Imported by: 0

README

Why TinySerializer?

It's Tiny!

As the title says, the package is not that big. It's written with about 500 lines of code, and only works on structs. It does not work on slices, maps or other types by themselves. It needs a struct to work on.

type TestStruct struct {
	ListMapStruct []map[int]*AllStruct `tiny:"listmapstruct"`
	BoolField     bool                 `tiny:"boolfield"`
	IntField      int64                `tiny:"intfield"`
	FloatField    float64              `tiny:"floatfield"`
	StringField   string               `tiny:"stringfield"`
	ListBool      []bool               `tiny:"listbool"`
	ListInt       []int64              `tiny:"listint"`
	ListFloat     []float64            `tiny:"listfloat"`
	ListString    []string             `tiny:"liststring"`
	MapBool       map[string]bool      `tiny:"mapbool"`
	MapInt        map[string]int64     `tiny:"mapint"`
	MapFloat      map[string]float64   `tiny:"mapfloat"`
	MapString     map[string]string    `tiny:"mapstring"`
	MapListBool   map[string][]bool    `tiny:"maplistbool"`
	MapListInt    map[string][]int64   `tiny:"maplistint"`
	MapListFloat  map[string][]float64 `tiny:"maplistfloat"`
	MapListString map[string][]string  `tiny:"mapliststring"`
	ListMapBool   []map[int]bool       `tiny:"listmapbool"`
	ListMapInt    []map[int]int64      `tiny:"listmapint"`
	ListMapFloat  []map[int]float64    `tiny:"listmapfloat"`
	ListMapString []map[int]string     `tiny:"listmapstring"`
}
Supports GZIP compression

Easily shrink your data by using GZIP compression. It's disabled by default, but can be enabled by using Serializer.SetCompress(true)

Example:

Create a serializer like so:

var err error
s := NewSerializer()
s, err = s.SetCompress(true)
if err != nil {
	panic(err)
}
data := s.Serialize(&teststruct) // Serialized data

And deserialize it like so:

deserialized := MyStruct{}
s = NewSerializer()
s.SetData(serialized)
s = s.SetCompress(true)
s.Deserialize(serialized, &TestStruct)
if err != nil {
	panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compress

func Compress(data []byte) ([]byte, error)

func Decompress

func Decompress(data []byte) ([]byte, error)

func GetBytes

func GetBytes(ndata []byte) ([]byte, error)

func GetValue

func GetValue(value reflect.Value) reflect.Value

Types

type Serializer

type Serializer struct {
	// contains filtered or unexported fields
}

Serializer is a struct that can serialize and deserialize data

func NewSerializer

func NewSerializer() *Serializer

NewSerializer creates a new serializer

func (*Serializer) CheckTag

func (s *Serializer) CheckTag(dataType reflect.Type, field reflect.Value, i int) bool

func (*Serializer) Deserialize

func (s *Serializer) Deserialize(data []byte, out interface{}) error

Deserialize deserializes the given data

func (*Serializer) Serialize

func (s *Serializer) Serialize(data interface{}) ([]byte, error)

Serialize serializes the given data

func (*Serializer) SetCompress

func (s *Serializer) SetCompress(compress bool) *Serializer

func (*Serializer) SetData

func (s *Serializer) SetData(data []byte) *Serializer

func (*Serializer) WriteField

func (s *Serializer) WriteField(field reflect.Value, kind reflect.Kind) error

func (*Serializer) WriteStruct

func (s *Serializer) WriteStruct(value reflect.Value, dataType reflect.Type) error

Jump to

Keyboard shortcuts

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