Giter VIP home page Giter VIP logo

go-cbor's Introduction

go-cbor

GitHub tag (latest SemVer) test Go Reference Go Report Card codecov

go-cobor provides encoders and decoders for Concise Binary Object Representation (CBOR) binary representations. CBOR is defined in RFC8949, and it is a data format whose design goals include the possibility of extremely small code size, fairly small message size, and extensibility without the need for version negotiation.

go-cobor was developed as a seamless serializer for the memory representation of any data types in Go like encodiong/json. go-cobor provides the optimized encoder and decoder to convert between CBOR and Go data models easily.

Although CBOR is designed for very small code size and fairly small message size, go-cobor encodes the specified data model without downgrading the data type to ensure seamless data model compatibility when decoding the encoded data.

Converting Data between Go and CBOR

go-cobor was developed as a seamless serializer for the memory representation of any data types in Go like encodiong/json. go-cobor provides the optimized encoder and decoder to convert between CBOR and Go data models easily.

Encoding - Converting from Go to CBOR

Decoder::Decode() and Marshal() convert from the specified data model of Go into the equivalent data model of CBOR as the following.

To convert data from Go to CBOR, go-cbor offers Marshal(). Marshal() converts from the specified data model of Go into the equivalent data model of CBOR. In addition to the basic Go data types, go-cbor supports additional tag major types such as time.Time as the following.

goTimeObj, _ := time.Parse(time.RFC3339, "2013-03-21T20:04:00Z")
goObjs := []any{
    uint(1000),
    int(-1000),
    float32(100000.0),
    float64(-4.1),
    false,
    true,
    nil,
    []byte("IETF"),
    "IETF",
    goTimeObj,
    []int{1, 2, 3},
    map[any]any{"a": "A"},
    struct {
        Key   string
        Value string
    }{
        Key: "hello", Value: "world",
    },
}
for _, goObj := range goObjs {
    cborBytes, _ := cbor.Marshal(goObj)
    fmt.Printf("%s\n", hex.EncodeToString(cborBytes))
}

Decoding - Converting from CBOR to Go

Decoder::Decode() and Unmarshal() convert from the specified data model of CBOR into the equivalent data model of Go as the following.

To convert data from CBOR to Go, go-cbor offers Unmarshal(). Unmarshal() converts from an encoded bytes of CBOR into the equivalent data model of Go as the following.

cborObjs := []string{
    "0a",
    "1903e8",
    "3903e7",
    "fb3ff199999999999a",
    "f90001",
    "f4",
    "f5",
    "f6",
    "c074323031332d30332d32315432303a30343a30305a",
    "4449455446",
    "6449455446",
    "83010203",
    "a201020304",
}
for _, cborObj := range cborObjs {
    cborBytes, _ := hex.DecodeString(cborObj)
    goObj, _ := cbor.Unmarshal(cborBytes)
    fmt.Printf("%s => %v\n", cborObj, goObj)
}

Unmarshaling from CBOR to Go

To unmarshal to a user-defined struct, go-cbor offers Decoder::Unmarshal() and UnmarshalTo(). The unmarshal functions try to convert from an encoded bytes of CBOR into the specified basic data types of Go as the following.

In addition to the basic standard data types of Go, The unmarshal functions support any user-defined maps and structs, as well as the standard struct such as time.Time as the following.

To unmarshal to a user-defined struct, go-cbor offers UnmarshalTo(). Unmarshal()To tries to convert from an encoded bytes of CBOR into the specified user-defined struct or map as the following.

examples := []struct {
    from any
    to   any
}{
    {
        from: []string{"one", "two"},
        to:   &[]string{},
    },
    {
        from: map[string]int{"one": 1, "two": 2},
        to:   map[string]int{},
    },
    {
        from: struct {
            Key   string
            Value string
        }{
            Key: "hello", Value: "world",
        },
        to: &struct {
            Key   string
            Value string
        }{},
    },
}

for _, e := range examples {
    encBytes, _ := cbor.Marshal(e.from)
    cbor.UnmarshalTo(encBytes, e.to)
    fmt.Printf("%v\n", e.to)
}

References

go-cbor's People

Contributors

cybergarage avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tinygo-pkg

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.