Convert Figma logo to code with AI

ugorji logogo

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]

1,895
299
1,895
2

Top Related Projects

[Deprecated] Protocol Buffers for Go with Gadgets

13,789

A high-performance 100% compatible drop-in replacement of "encoding/json"

Fast JSON serializer for golang.

2,979

faster JSON serialization for Go

2,500

msgpack.org[Go] MessagePack encoding for Golang

Quick Overview

ugorji/go is a high-performance, feature-rich codec and serialization library for Go. It provides efficient encoding and decoding for various formats, including MessagePack, CBOR, JSON, and more. The library is designed to be fast, flexible, and easy to use for Go developers.

Pros

  • High performance and efficiency in encoding/decoding
  • Supports multiple serialization formats (MessagePack, CBOR, JSON, etc.)
  • Extensive customization options and configuration
  • Well-maintained and actively developed

Cons

  • Steeper learning curve compared to standard library encoding/json
  • Large API surface area may be overwhelming for simple use cases
  • Some users report occasional breaking changes between versions
  • Documentation could be more comprehensive for advanced features

Code Examples

  1. Basic encoding and decoding using MessagePack:
import (
    "fmt"
    "github.com/ugorji/go/codec"
)

func main() {
    var (
        mh codec.MsgpackHandle
        b  []byte
        h  = &mh
    )

    type testStruct struct {
        Name string
        Age  int
    }

    // Encoding
    v := testStruct{"John Doe", 30}
    enc := codec.NewEncoderBytes(&b, h)
    err := enc.Encode(v)
    if err != nil {
        panic(err)
    }

    // Decoding
    var v2 testStruct
    dec := codec.NewDecoderBytes(b, h)
    err = dec.Decode(&v2)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%v\n", v2)
}
  1. Using JSON with custom encoding options:
import (
    "fmt"
    "github.com/ugorji/go/codec"
)

func main() {
    var (
        jh codec.JsonHandle
        b  []byte
    )
    jh.Indent = 2
    jh.HTMLCharsAsIs = true

    v := map[string]interface{}{
        "name": "Alice <alice@example.com>",
        "age":  25,
    }

    enc := codec.NewEncoderBytes(&b, &jh)
    err := enc.Encode(v)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(b))
}
  1. Using CBOR with a custom tag:
import (
    "fmt"
    "github.com/ugorji/go/codec"
)

func main() {
    var (
        ch codec.CborHandle
        b  []byte
    )

    type CustomType struct {
        Value string `codec:"1,omitempty"`
    }

    v := CustomType{"Hello, CBOR!"}

    enc := codec.NewEncoderBytes(&b, &ch)
    err := enc.Encode(v)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%x\n", b)
}

Getting Started

To use ugorji/go in your Go project:

  1. Install the library:

    go get github.com/ugorji/go/codec
    
  2. Import the package in your Go code:

    import "github.com/ugorji/go/codec"
    
  3. Choose a codec handle (e.g., MsgpackHandle, JsonHandle, CborHandle) based on your desired format.

  4. Create an encoder or decoder using NewEncoder, NewDecoder, NewEncoderBytes, or NewDecoderBytes.

  5. Use the Encode or Decode methods to serialize or deserialize your data.

Refer to the code examples above for more detailed usage patterns.

Competitor Comparisons

[Deprecated] Protocol Buffers for Go with Gadgets

Pros of protobuf

  • Optimized for Protocol Buffers, offering better performance for protobuf-specific use cases
  • Provides additional code generation options and customizations
  • Includes extensions for gRPC support

Cons of protobuf

  • More complex setup and usage compared to go
  • Limited to Protocol Buffers, while go supports multiple serialization formats
  • May have compatibility issues with standard protobuf implementations

Code Comparison

protobuf:

import "github.com/gogo/protobuf/proto"

data, err := proto.Marshal(&myProtoMessage)

go:

import "github.com/ugorji/go/codec"

var h codec.MsgpackHandle
err := codec.NewEncoderBytes(&data, &h).Encode(myStruct)

Summary

protobuf is specialized for Protocol Buffers, offering optimized performance and additional features for protobuf-specific use cases. It provides more customization options and gRPC support. However, it's more complex to set up and use compared to go.

go is a more versatile serialization library, supporting multiple formats beyond Protocol Buffers. It's simpler to use and integrate but may not offer the same level of performance optimization for protobuf-specific scenarios.

Choose protobuf if you're working extensively with Protocol Buffers and need advanced features or optimizations. Opt for go if you require a more flexible serialization solution supporting multiple formats with a simpler implementation.

13,789

A high-performance 100% compatible drop-in replacement of "encoding/json"

Pros of json-iterator/go

  • Significantly faster performance, especially for large JSON payloads
  • More memory-efficient, with lower allocation rates
  • Supports custom extensions for tailored JSON handling

Cons of json-iterator/go

  • Less mature and potentially less stable than go
  • May have fewer features and customization options
  • Smaller community and ecosystem compared to go

Code Comparison

go:

import "encoding/json"

var data map[string]interface{}
err := json.Unmarshal([]byte(jsonString), &data)

json-iterator/go:

import jsoniter "github.com/json-iterator/go"

var json = jsoniter.ConfigCompatibleWithStandardLibrary
var data map[string]interface{}
err := json.Unmarshal([]byte(jsonString), &data)

Both libraries offer similar APIs, making it easy to switch between them. The main difference lies in the import statement and the initialization of the JSON parser.

json-iterator/go provides a performance boost over go, especially for large JSON payloads. However, go is more established and may offer better stability and a wider range of features. The choice between the two depends on specific project requirements, with json-iterator/go being particularly suitable for performance-critical applications dealing with large JSON datasets.

Fast JSON serializer for golang.

Pros of easyjson

  • Generates code for faster JSON serialization/deserialization
  • Supports custom MarshalJSON/UnmarshalJSON methods
  • Allows for easy integration with existing Go types

Cons of easyjson

  • Requires code generation step, which can complicate build process
  • Limited support for complex types and nested structures
  • May produce larger binary sizes due to generated code

Code Comparison

easyjson:

//easyjson:json
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

go:

type Person struct {
    Name string `codec:"name"`
    Age  int    `codec:"age"`
}

Key Differences

  • easyjson focuses on JSON serialization, while go supports multiple codecs
  • easyjson generates code for performance, go uses reflection-based approach
  • easyjson requires struct tags and code generation, go is more flexible with tags

Use Cases

  • easyjson: High-performance JSON handling in Go applications
  • go: Multi-format serialization needs with flexibility in codec choices

Community and Maintenance

  • Both projects are actively maintained and have significant community adoption
  • easyjson has more focused scope, while go offers broader serialization options

Performance

  • easyjson generally offers better performance for JSON operations
  • go provides good performance across multiple serialization formats
2,979

faster JSON serialization for Go

Pros of ffjson

  • Generates code for faster JSON serialization and deserialization
  • Optimized for performance, especially for large structs
  • Supports custom marshaler and unmarshaler interfaces

Cons of ffjson

  • Requires code generation step, which can complicate build processes
  • Limited support for complex types and nested structures
  • Less actively maintained compared to go

Code Comparison

ffjson:

//go:generate ffjson $GOFILE
type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

go:

type Person struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

// No code generation required

Key Differences

  • ffjson focuses on performance through code generation
  • go offers a more flexible and feature-rich JSON encoding/decoding solution
  • ffjson requires an additional build step, while go works out of the box
  • go has broader community support and more frequent updates
  • ffjson may provide better performance for specific use cases, especially with large structs

Use Cases

  • Choose ffjson for projects where JSON performance is critical and struct definitions are relatively stable
  • Opt for go in general-purpose applications, where flexibility and ease of use are prioritized over raw performance
2,500

msgpack.org[Go] MessagePack encoding for Golang

Pros of msgpack

  • Better performance for small messages
  • More actively maintained with frequent updates
  • Supports custom types and interfaces out of the box

Cons of msgpack

  • Less flexible API compared to go
  • May have slightly larger binary size due to reflection usage
  • Limited support for streaming large datasets

Code Comparison

msgpack:

type Item struct {
    Name  string
    Value int
}

item := Item{Name: "example", Value: 42}
b, err := msgpack.Marshal(item)

go:

type Item struct {
    Name  string
    Value int
}

item := Item{Name: "example", Value: 42}
var h codec.Handle = new(codec.MsgpackHandle)
var b []byte
err := codec.NewEncoderBytes(&b, h).Encode(item)

Both libraries provide MessagePack encoding and decoding for Go, but they differ in their approach and feature set. msgpack offers a simpler API and better performance for small messages, while go provides more flexibility and control over the encoding process. The choice between them depends on specific project requirements, such as performance needs, message complexity, and desired level of customization.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Sourcegraph Build and Test go-codec codecov Go Reference rcard License

go-codec

This repository contains the go-codec library, the codecgen tool and benchmarks for comparing against other libraries.

This is a High Performance, Feature-Rich Idiomatic Go 1.20+ codec/encoding library for binary and text formats: binc, msgpack, cbor, json and simple.

It supports generics and monomorphization, based on build tags set.

Code Organization and Module Support

This repository consists of 1 module:

  • github.com/ugorji/go/codec README

To install:

go get github.com/ugorji/go/codec