Git Product home page Git Product logo

binary's Introduction

Generic and Fast Binary Serializer for Go

This repository contains a fast binary packer for Golang, this allows to encode/decode arbtitrary golang data structures of variable size. Documentation can be found on https://godoc.org/github.com/Kelindar/binary.

This package extends support to arbitrary, variable-sized values by prefixing these values with their varint-encoded size, recursively. This was originally inspired by Alec Thomas's binary package, but I've reworked the serialization format and improved the performance and size. Here's a few notable features/goals of this binary package:

  • Zero-allocation encoding. I'm hoping to make the encoding to be as fast as possible, simply writing binary to the io.Writer without unncessary allocations.
  • Support for maps, arrays, slices, structs, primitive and nested types.
  • This is essentially a json.Marshal and json.Unmarshal drop-in replacement, I wanted this package to be simple to use and leverage the power of reflect package of golang.
  • The ints and uints are encoded using varint, making the payload small as possible.
  • Fast-paths encoding and decoding of []byte, as I've designed this package to be used for inter-broker message encoding for emitter.
  • Support for custom BinaryMarshaler and BinaryUnmarshaler for tighter packing control and built-in types such as time.Time.

Usage

To serialize a message, simply Marshal:

v := &message{
    Name:      "Roman",
    Timestamp: 1242345235,
    Payload:   []byte("hi"),
    Ssid:      []uint32{1, 2, 3},
}

encoded, err := binary.Marshal(v)

To deserialize, Unmarshal:

var v message
err := binary.Unmarshal(encoded, &v)

Disclaimer

This is not intended as a replacement for JSON or protobuf, this codec does not maintain any versioning or compatibility - and not intended to become one. The goal of this binary codec is to efficiently exchange binary data of known format between systems where you control both ends and both of them are written in Go.

binary's People

Contributors

kelindar 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.