Git Product home page Git Product logo

serializer's Introduction

serializer

Package serializer makes it easy to serialize and deserialize objects across instances of a Go application.

To see how to use this package, you can check out the GoDoc.

Why you need it

Go already gives you encoding/json and encoding/gob, so why do you need this new serialization package? You may not, depending on your use case.

JSON and Gob are great for many situations. JSON is great when you need a cross-platform data format, and Gob is great for sending Go objects over a network. Unfortunately, Gob is not intended to be used as a data storage format; when you use Gob, the data you store depends too much on the layout of the Go structures. JSON, while intended to store data, doesn't work very well with type information. Take this example:

type ActivationFunc interface {
    Eval(x float64) float64
}

type SerializeMe struct {
    X  int
    A  ActivationFunc
    As []ActivationFunc
}

The json package would succeed at serializing an instance of SerializeMe, provided that the ActivationFuncs didn't have cyclic structure. However, it would run into trouble trying to deserialize the same instance, since it would have no way to create new instances of ActivationFunc.

How it works

Serializer tries to retain type information while remaining suitable for data storage. To deal with type information, serializer uses type IDs. Any object that implements the Serializer interface has a type ID, and this type ID must be registered in a table of decoder functions. This way, any object that can be encoded can also be decoded, since the decoder function knows how to create a new instance of the encoded type.

To keep serialization cross-platform and suitable for data storage, serialization is implemented on a per-type basis. A Serializer implements a Serialize method which manually encodes the object as binary. The package provides various serialization helpers, but ultimately it is up to an object to decide how it's laid out as data. This makes serializer more cross-platform than Gob in many cases.

serializer's People

Contributors

unixpickle avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.