Convert Figma logo to code with AI

francoispqt logogojay

high performance JSON encoder/decoder with stream API for Golang

2,106
112
2,106
48

Top Related Projects

One of the fastest alternative JSON parser for Go that does not require schema

13,330

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

Fast JSON serializer for golang.

14,239

Get JSON values quickly - JSON parser for Go

2,961

faster JSON serialization for Go

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

Quick Overview

Gojay is a high-performance JSON encoder/decoder for Go. It aims to be the fastest JSON library for Go, offering both ease of use and flexibility. Gojay provides a simple API for encoding and decoding JSON data, with a focus on performance and low memory allocation.

Pros

  • Extremely fast JSON encoding and decoding
  • Low memory allocation, reducing garbage collection overhead
  • Supports both struct-based and interface-based encoding/decoding
  • Provides streaming capabilities for large JSON datasets

Cons

  • Less feature-rich compared to some other JSON libraries
  • May require more manual work for complex JSON structures
  • Limited support for custom types without implementing specific interfaces
  • Relatively newer library compared to more established alternatives

Code Examples

Encoding a struct to JSON:

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

user := User{Name: "John Doe", Age: 30}
json, _ := gojay.Marshal(&user)
fmt.Println(string(json))

Decoding JSON to a struct:

jsonData := []byte(`{"name":"Jane Doe","age":25}`)
var user User
err := gojay.UnmarshalJSONObject(jsonData, &user)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Printf("%+v\n", user)
}

Streaming large JSON arrays:

dec := gojay.NewDecoder(reader)
err := dec.DecodeArray(func(dec *gojay.Decoder) error {
    var item MyItem
    if err := dec.Object(&item); err != nil {
        return err
    }
    // Process item
    return nil
})

Getting Started

To use Gojay in your Go project, follow these steps:

  1. Install Gojay:

    go get github.com/francoispqt/gojay
    
  2. Import Gojay in your Go file:

    import "github.com/francoispqt/gojay"
    
  3. Use Gojay's Marshal and Unmarshal functions for basic JSON operations, or implement the MarshalerJSONObject and UnmarshalerJSONObject interfaces for custom types to take full advantage of Gojay's performance benefits.

Competitor Comparisons

One of the fastest alternative JSON parser for Go that does not require schema

Pros of jsonparser

  • Faster performance for parsing large JSON files
  • Lower memory usage due to zero-allocation design
  • Simpler API for basic JSON parsing tasks

Cons of jsonparser

  • Limited functionality compared to full-featured JSON libraries
  • Lacks encoding capabilities (only focuses on parsing)
  • May require more manual work for complex JSON structures

Code Comparison

jsonparser:

value, err := jsonparser.GetString(data, "user", "name")
if err != nil {
    log.Fatal(err)
}
fmt.Println(value)

gojay:

var user struct {
    Name string `json:"name"`
}
err := gojay.UnmarshalJSONObject(data, &user)
if err != nil {
    log.Fatal(err)
}
fmt.Println(user.Name)

jsonparser provides a more direct approach to accessing specific JSON fields, while gojay offers a more structured object-oriented approach with unmarshaling capabilities. jsonparser is generally faster and more memory-efficient for simple parsing tasks, but gojay provides a more comprehensive JSON handling solution with both encoding and decoding features. The choice between the two depends on the specific requirements of your project, such as performance needs, memory constraints, and the complexity of JSON operations required.

13,330

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

Pros of json-iterator/go

  • Higher performance in most benchmarks, especially for large JSON payloads
  • More comprehensive feature set, including support for custom types and interfaces
  • Better compatibility with the standard library's encoding/json package

Cons of json-iterator/go

  • Slightly more complex API compared to gojay's streamlined approach
  • Larger codebase and dependency footprint
  • May require more configuration for optimal performance in specific use cases

Code Comparison

json-iterator/go:

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

var json = jsoniter.ConfigCompatibleWithStandardLibrary
data := json.Marshal(&someStruct)

gojay:

import "github.com/francoispqt/gojay"

data, _ := gojay.Marshal(&someStruct)

Summary

json-iterator/go offers higher performance and more features, making it suitable for complex JSON handling scenarios. It maintains compatibility with the standard library, which can ease integration into existing projects. However, its API is slightly more complex, and it has a larger codebase.

gojay provides a simpler API and a more lightweight implementation, which may be preferable for projects with straightforward JSON needs or where minimizing dependencies is crucial. However, it may not perform as well as json-iterator/go for large JSON payloads and lacks some advanced features.

Choose json-iterator/go for high-performance, feature-rich JSON handling, especially in larger projects. Opt for gojay if simplicity and a smaller footprint are priorities, and your JSON processing needs are relatively basic.

Fast JSON serializer for golang.

Pros of easyjson

  • Generates code for faster JSON encoding/decoding, potentially offering better performance
  • Supports custom MarshalJSON/UnmarshalJSON methods for fine-grained control
  • Integrates well with Go's standard encoding/json package

Cons of easyjson

  • Requires code generation step, which can complicate build processes
  • May produce larger binary sizes due to generated code
  • Less flexible for dynamic JSON structures compared to gojay

Code Comparison

easyjson:

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

gojay:

type Person struct {
    Name string
    Age  int
}

func (p *Person) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
    switch k {
    case "name":
        return dec.String(&p.Name)
    case "age":
        return dec.Int(&p.Age)
    }
    return nil
}

Both libraries aim to improve JSON handling performance in Go, but they take different approaches. easyjson focuses on code generation for static structures, while gojay provides a more flexible, runtime-based approach. The choice between them depends on specific project requirements, such as performance needs, build complexity tolerance, and the nature of JSON data being handled.

14,239

Get JSON values quickly - JSON parser for Go

Pros of gjson

  • Simpler API, focused on JSON querying and extraction
  • Supports a powerful path syntax for navigating complex JSON structures
  • No external dependencies, lightweight and easy to integrate

Cons of gjson

  • Limited to JSON parsing and querying, lacks encoding capabilities
  • May be less performant for large-scale JSON processing compared to gojay

Code Comparison

gjson:

name := gjson.Get(json, "name").String()
age := gjson.Get(json, "age").Int()

gojay:

var user User
err := gojay.UnmarshalJSONObject([]byte(json), &user)

Key Differences

  • gjson focuses on JSON querying and extraction, while gojay provides both encoding and decoding capabilities
  • gjson uses a path-based approach for accessing JSON data, whereas gojay employs struct-based unmarshaling
  • gojay offers more advanced features like stream decoding and custom marshalers, making it suitable for complex JSON processing tasks

Use Cases

  • Choose gjson for simple JSON querying and extraction tasks
  • Opt for gojay when working with large JSON datasets or requiring both encoding and decoding functionalities
2,961

faster JSON serialization for Go

Pros of ffjson

  • Generates Go code for faster JSON serialization and deserialization
  • Supports custom MarshalJSON and UnmarshalJSON methods
  • Provides a command-line tool for easy code generation

Cons of ffjson

  • Requires code generation step, which can complicate build processes
  • May not perform as well as gojay for certain use cases
  • Less actively maintained compared to gojay

Code Comparison

ffjson:

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

// Generated code
func (u *User) MarshalJSON() ([]byte, error) {
    // Custom marshaling logic
}

gojay:

type User struct {
    Name string
    Age  int
}

func (u *User) MarshalJSONObject(enc *gojay.Encoder) {
    enc.StringKey("name", u.Name)
    enc.IntKey("age", u.Age)
}

ffjson generates custom marshaling code, while gojay uses a more explicit approach with custom methods. gojay's method allows for more fine-grained control over the serialization process, but may require more manual implementation. ffjson's generated code can be more convenient for simple structures but may be less flexible for complex scenarios.

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

Pros of fastjson

  • Higher performance for parsing and serialization
  • Lower memory allocation and garbage collection overhead
  • Supports streaming JSON parsing for large datasets

Cons of fastjson

  • Less feature-rich API compared to gojay
  • Fewer convenience functions for common operations
  • Steeper learning curve for beginners

Code Comparison

fastjson:

var p fastjson.Parser
v, err := p.Parse(jsonStr)
if err != nil {
    return err
}
name := v.GetStringBytes("name")

gojay:

user := User{}
err := gojay.UnmarshalJSONObject([]byte(jsonStr), &user)
if err != nil {
    return err
}
name := user.Name

fastjson focuses on raw performance and low-level operations, while gojay provides a more user-friendly API with struct marshaling and unmarshaling. fastjson is generally faster but requires more manual handling of JSON data. gojay offers a more convenient interface at the cost of some performance. The choice between the two depends on specific project requirements, with fastjson being better suited for high-performance scenarios and gojay for ease of use and rapid development.

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

Build Status codecov Go Report Card Go doc MIT License Sourcegraph stability-stable

GoJay

GoJay is a performant JSON encoder/decoder for Golang (currently the most performant, see benchmarks).

It has a simple API and doesn't use reflection. It relies on small interfaces to decode/encode structures and slices.

Gojay also comes with powerful stream decoding features and an even faster Unsafe API.

There is also a code generation tool to make usage easier and faster.

Why another JSON parser?

I looked at other fast decoder/encoder and realised it was mostly hardly readable static code generation or a lot of reflection, poor streaming features, and not so fast in the end.

Also, I wanted to build a decoder that could consume an io.Reader of line or comma delimited JSON, in a JIT way. To consume a flow of JSON objects from a TCP connection for example or from a standard output. Same way I wanted to build an encoder that could encode a flow of data to a io.Writer.

This is how GoJay aims to be a very fast, JIT stream parser with 0 reflection, low allocation with a friendly API.

Get started

go get github.com/francoispqt/gojay

Decoding

Decoding is done through two different API similar to standard encoding/json:

Example of basic stucture decoding with Unmarshal:

import "github.com/francoispqt/gojay"

type user struct {
    id int
    name string
    email string
}
// implement gojay.UnmarshalerJSONObject
func (u *user) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
    switch key {
    case "id":
        return dec.Int(&u.id)
    case "name":
        return dec.String(&u.name)
    case "email":
        return dec.String(&u.email)
    }
    return nil
}
func (u *user) NKeys() int {
    return 3
}

func main() {
    u := &user{}
    d := []byte(`{"id":1,"name":"gojay","email":"gojay@email.com"}`)
    err := gojay.UnmarshalJSONObject(d, u)
    if err != nil {
        log.Fatal(err)
    }
}

with Decode:

func main() {
    u := &user{}
    dec := gojay.NewDecoder(bytes.NewReader([]byte(`{"id":1,"name":"gojay","email":"gojay@email.com"}`)))
    err := dec.DecodeObject(d, u)
    if err != nil {
        log.Fatal(err)
    }
}

Unmarshal API

Unmarshal API decodes a []byte to a given pointer with a single function.

Behind the doors, Unmarshal API borrows a *gojay.Decoder resets its settings and decodes the data to the given pointer and releases the *gojay.Decoder to the pool when it finishes, whether it encounters an error or not.

If it cannot find the right Decoding strategy for the type of the given pointer, it returns an InvalidUnmarshalError. You can test the error returned by doing if ok := err.(InvalidUnmarshalError); ok {}.

Unmarshal API comes with three functions:

  • Unmarshal
func Unmarshal(data []byte, v interface{}) error
  • UnmarshalJSONObject
func UnmarshalJSONObject(data []byte, v gojay.UnmarshalerJSONObject) error
  • UnmarshalJSONArray
func UnmarshalJSONArray(data []byte, v gojay.UnmarshalerJSONArray) error

Decode API

Decode API decodes a []byte to a given pointer by creating or borrowing a *gojay.Decoder with an io.Reader and calling Decode methods.

Getting a *gojay.Decoder or Borrowing

You can either get a fresh *gojay.Decoder calling dec := gojay.NewDecoder(io.Reader) or borrow one from the pool by calling dec := gojay.BorrowDecoder(io.Reader).

After using a decoder, you can release it by calling dec.Release(). Beware, if you reuse the decoder after releasing it, it will panic with an error of type InvalidUsagePooledDecoderError. If you want to fully benefit from the pooling, you must release your decoders after using.

Example getting a fresh an releasing:

str := ""
dec := gojay.NewDecoder(strings.NewReader(`"test"`))
defer dec.Release()
if err := dec.Decode(&str); err != nil {
    log.Fatal(err)
}

Example borrowing a decoder and releasing:

str := ""
dec := gojay.BorrowDecoder(strings.NewReader(`"test"`))
defer dec.Release()
if err := dec.Decode(&str); err != nil {
    log.Fatal(err)
}

*gojay.Decoder has multiple methods to decode to specific types:

  • Decode
func (dec *gojay.Decoder) Decode(v interface{}) error
  • DecodeObject
func (dec *gojay.Decoder) DecodeObject(v gojay.UnmarshalerJSONObject) error
  • DecodeArray
func (dec *gojay.Decoder) DecodeArray(v gojay.UnmarshalerJSONArray) error
  • DecodeInt
func (dec *gojay.Decoder) DecodeInt(v *int) error
  • DecodeBool
func (dec *gojay.Decoder) DecodeBool(v *bool) error
  • DecodeString
func (dec *gojay.Decoder) DecodeString(v *string) error

All DecodeXxx methods are used to decode top level JSON values. If you are decoding keys or items of a JSON object or array, don't use the Decode methods.

Example:

reader := strings.NewReader(`"John Doe"`)
dec := NewDecoder(reader)

var str string
err := dec.DecodeString(&str)
if err != nil {
    log.Fatal(err)
}

fmt.Println(str) // John Doe

Structs and Maps

UnmarshalerJSONObject Interface

To unmarshal a JSON object to a structure, the structure must implement the UnmarshalerJSONObject interface:

type UnmarshalerJSONObject interface {
	UnmarshalJSONObject(*gojay.Decoder, string) error
	NKeys() int
}

UnmarshalJSONObject method takes two arguments, the first one is a pointer to the Decoder (*gojay.Decoder) and the second one is the string value of the current key being parsed. If the JSON data is not an object, the UnmarshalJSONObject method will never be called.

NKeys method must return the number of keys to Unmarshal in the JSON object or 0. If zero is returned, all keys will be parsed.

Example of implementation for a struct:

type user struct {
    id int
    name string
    email string
}
// implement UnmarshalerJSONObject
func (u *user) UnmarshalJSONObject(dec *gojay.Decoder, key string) error {
    switch key {
    case "id":
        return dec.Int(&u.id)
    case "name":
        return dec.String(&u.name)
    case "email":
        return dec.String(&u.email)
    }
    return nil
}
func (u *user) NKeys() int {
    return 3
}

Example of implementation for a map[string]string:

// define our custom map type implementing UnmarshalerJSONObject
type message map[string]string

// Implementing Unmarshaler
func (m message) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
	str := ""
	err := dec.String(&str)
	if err != nil {
		return err
	}
	m[k] = str
	return nil
}

// we return 0, it tells the Decoder to decode all keys
func (m message) NKeys() int {
	return 0
}

Arrays, Slices and Channels

To unmarshal a JSON object to a slice an array or a channel, it must implement the UnmarshalerJSONArray interface:

type UnmarshalerJSONArray interface {
	UnmarshalJSONArray(*gojay.Decoder) error
}

UnmarshalJSONArray method takes one argument, a pointer to the Decoder (*gojay.Decoder). If the JSON data is not an array, the Unmarshal method will never be called.

Example of implementation with a slice:

type testSlice []string
// implement UnmarshalerJSONArray
func (t *testSlice) UnmarshalJSONArray(dec *gojay.Decoder) error {
	str := ""
	if err := dec.String(&str); err != nil {
		return err
	}
	*t = append(*t, str)
	return nil
}

func main() {
	dec := gojay.BorrowDecoder(strings.NewReader(`["Tom", "Jim"]`))
	var slice testSlice
	err := dec.DecodeArray(&slice)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(slice) // [Tom Jim]
	dec.Release()
}

Example of implementation with a channel:

type testChannel chan string
// implement UnmarshalerJSONArray
func (c testChannel) UnmarshalJSONArray(dec *gojay.Decoder) error {
	str := ""
	if err := dec.String(&str); err != nil {
		return err
	}
	c <- str
	return nil
}

func main() {
	dec := gojay.BorrowDecoder(strings.NewReader(`["Tom", "Jim"]`))
	c := make(testChannel, 2)
	err := dec.DecodeArray(c)
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; i < 2; i++ {
		fmt.Println(<-c)
	}
	close(c)
	dec.Release()
}

Example of implementation with an array:

type testArray [3]string
// implement UnmarshalerJSONArray
func (a *testArray) UnmarshalJSONArray(dec *Decoder) error {
	var str string
	if err := dec.String(&str); err != nil {
		return err
	}
	a[dec.Index()] = str
	return nil
}

func main() {
	dec := gojay.BorrowDecoder(strings.NewReader(`["Tom", "Jim", "Bob"]`))
	var a testArray
	err := dec.DecodeArray(&a)
	fmt.Println(a) // [Tom Jim Bob]
	dec.Release()
}

Other types

To decode other types (string, int, int32, int64, uint32, uint64, float, booleans), you don't need to implement any interface.

Example of encoding strings:

func main() {
    json := []byte(`"Jay"`)
    var v string
    err := gojay.Unmarshal(json, &v)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(v) // Jay
}

Decode values methods

When decoding a JSON object of a JSON array using UnmarshalerJSONObject or UnmarshalerJSONArray interface, the gojay.Decoder provides dozens of methods to Decode multiple types.

Non exhaustive list of methods available (to see all methods, check the godoc):

dec.Int
dec.Int8
dec.Int16
dec.Int32
dec.Int64
dec.Uint8
dec.Uint16
dec.Uint32
dec.Uint64
dec.String
dec.Time
dec.Bool
dec.SQLNullString
dec.SQLNullInt64

Encoding

Encoding is done through two different API similar to standard encoding/json:

Example of basic structure encoding with Marshal:

import "github.com/francoispqt/gojay"

type user struct {
	id    int
	name  string
	email string
}

// implement MarshalerJSONObject
func (u *user) MarshalJSONObject(enc *gojay.Encoder) {
	enc.IntKey("id", u.id)
	enc.StringKey("name", u.name)
	enc.StringKey("email", u.email)
}
func (u *user) IsNil() bool {
	return u == nil
}

func main() {
	u := &user{1, "gojay", "gojay@email.com"}
	b, err := gojay.MarshalJSONObject(u)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b)) // {"id":1,"name":"gojay","email":"gojay@email.com"}
}

with Encode:

func main() {
	u := &user{1, "gojay", "gojay@email.com"}
	b := strings.Builder{}
	enc := gojay.NewEncoder(&b)
	if err := enc.Encode(u); err != nil {
		log.Fatal(err)
	}
	fmt.Println(b.String()) // {"id":1,"name":"gojay","email":"gojay@email.com"}
}

Marshal API

Marshal API encodes a value to a JSON []byte with a single function.

Behind the doors, Marshal API borrows a *gojay.Encoder resets its settings and encodes the data to an internal byte buffer and releases the *gojay.Encoder to the pool when it finishes, whether it encounters an error or not.

If it cannot find the right Encoding strategy for the type of the given value, it returns an InvalidMarshalError. You can test the error returned by doing if ok := err.(InvalidMarshalError); ok {}.

Marshal API comes with three functions:

  • Marshal
func Marshal(v interface{}) ([]byte, error)
  • MarshalJSONObject
func MarshalJSONObject(v gojay.MarshalerJSONObject) ([]byte, error)
  • MarshalJSONArray
func MarshalJSONArray(v gojay.MarshalerJSONArray) ([]byte, error)

Encode API

Encode API decodes a value to JSON by creating or borrowing a *gojay.Encoder sending it to an io.Writer and calling Encode methods.

Getting a *gojay.Encoder or Borrowing

You can either get a fresh *gojay.Encoder calling enc := gojay.NewEncoder(io.Writer) or borrow one from the pool by calling enc := gojay.BorrowEncoder(io.Writer).

After using an encoder, you can release it by calling enc.Release(). Beware, if you reuse the encoder after releasing it, it will panic with an error of type InvalidUsagePooledEncoderError. If you want to fully benefit from the pooling, you must release your encoders after using.

Example getting a fresh encoder an releasing:

str := "test"
b := strings.Builder{}
enc := gojay.NewEncoder(&b)
defer enc.Release()
if err := enc.Encode(str); err != nil {
    log.Fatal(err)
}

Example borrowing an encoder and releasing:

str := "test"
b := strings.Builder{}
enc := gojay.BorrowEncoder(b)
defer enc.Release()
if err := enc.Encode(str); err != nil {
    log.Fatal(err)
}

*gojay.Encoder has multiple methods to encoder specific types to JSON:

  • Encode
func (enc *gojay.Encoder) Encode(v interface{}) error
  • EncodeObject
func (enc *gojay.Encoder) EncodeObject(v gojay.MarshalerJSONObject) error
  • EncodeArray
func (enc *gojay.Encoder) EncodeArray(v gojay.MarshalerJSONArray) error
  • EncodeInt
func (enc *gojay.Encoder) EncodeInt(n int) error
  • EncodeInt64
func (enc *gojay.Encoder) EncodeInt64(n int64) error
  • EncodeFloat
func (enc *gojay.Encoder) EncodeFloat(n float64) error
  • EncodeBool
func (enc *gojay.Encoder) EncodeBool(v bool) error
  • EncodeString
func (enc *gojay.Encoder) EncodeString(s string) error

Structs and Maps

To encode a structure, the structure must implement the MarshalerJSONObject interface:

type MarshalerJSONObject interface {
	MarshalJSONObject(enc *gojay.Encoder)
	IsNil() bool
}

MarshalJSONObject method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all the keys in the JSON Object by calling Decoder's methods.

IsNil method returns a boolean indicating if the interface underlying value is nil or not. It is used to safely ensure that the underlying value is not nil without using Reflection.

Example of implementation for a struct:

type user struct {
	id    int
	name  string
	email string
}

// implement MarshalerJSONObject
func (u *user) MarshalJSONObject(enc *gojay.Encoder) {
	enc.IntKey("id", u.id)
	enc.StringKey("name", u.name)
	enc.StringKey("email", u.email)
}
func (u *user) IsNil() bool {
	return u == nil
}

Example of implementation for a map[string]string:

// define our custom map type implementing MarshalerJSONObject
type message map[string]string

// Implementing Marshaler
func (m message) MarshalJSONObject(enc *gojay.Encoder) {
	for k, v := range m {
		enc.StringKey(k, v)
	}
}

func (m message) IsNil() bool {
	return m == nil
}

Arrays and Slices

To encode an array or a slice, the slice/array must implement the MarshalerJSONArray interface:

type MarshalerJSONArray interface {
	MarshalJSONArray(enc *gojay.Encoder)
	IsNil() bool
}

MarshalJSONArray method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all element in the JSON Array by calling Decoder's methods.

IsNil method returns a boolean indicating if the interface underlying value is nil(empty) or not. It is used to safely ensure that the underlying value is not nil without using Reflection and also to in OmitEmpty feature.

Example of implementation:

type users []*user
// implement MarshalerJSONArray
func (u *users) MarshalJSONArray(enc *gojay.Encoder) {
	for _, e := range u {
		enc.Object(e)
	}
}
func (u *users) IsNil() bool {
	return len(u) == 0
}

Other types

To encode other types (string, int, float, booleans), you don't need to implement any interface.

Example of encoding strings:

func main() {
	name := "Jay"
	b, err := gojay.Marshal(name)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b)) // "Jay"
}

Stream API

Stream Decoding

GoJay ships with a powerful stream decoder.

It allows to read continuously from an io.Reader stream and do JIT decoding writing unmarshalled JSON to a channel to allow async consuming.

When using the Stream API, the Decoder implements context.Context to provide graceful cancellation.

To decode a stream of JSON, you must call gojay.Stream.DecodeStream and pass it a UnmarshalerStream implementation.

type UnmarshalerStream interface {
	UnmarshalStream(*StreamDecoder) error
}

Example of implementation of stream reading from a WebSocket connection:

// implement UnmarshalerStream
type ChannelStream chan *user

func (c ChannelStream) UnmarshalStream(dec *gojay.StreamDecoder) error {
	u := &user{}
	if err := dec.Object(u); err != nil {
		return err
	}
	c <- u
	return nil
}

func main() {
	// get our websocket connection
	origin := "http://localhost/"
	url := "ws://localhost:12345/ws"
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		log.Fatal(err)
	}
	// create our channel which will receive our objects
	streamChan := ChannelStream(make(chan *user))
	// borrow a decoder
	dec := gojay.Stream.BorrowDecoder(ws)
	// start decoding, it will block until a JSON message is decoded from the WebSocket
	// or until Done channel is closed
	go dec.DecodeStream(streamChan)
	for {
		select {
		case v := <-streamChan:
			// Got something from my websocket!
			log.Println(v)
		case <-dec.Done():
			log.Println("finished reading from WebSocket")
			os.Exit(0)
		}
	}
}

Stream Encoding

GoJay ships with a powerful stream encoder part of the Stream API.

It allows to write continuously to an io.Writer and do JIT encoding of data fed to a channel to allow async consuming. You can set multiple consumers on the channel to be as performant as possible. Consumers are non blocking and are scheduled individually in their own go routine.

When using the Stream API, the Encoder implements context.Context to provide graceful cancellation.

To encode a stream of data, you must call EncodeStream and pass it a MarshalerStream implementation.

type MarshalerStream interface {
	MarshalStream(enc *gojay.StreamEncoder)
}

Example of implementation of stream writing to a WebSocket:

// Our structure which will be pushed to our stream
type user struct {
	id    int
	name  string
	email string
}

func (u *user) MarshalJSONObject(enc *gojay.Encoder) {
	enc.IntKey("id", u.id)
	enc.StringKey("name", u.name)
	enc.StringKey("email", u.email)
}
func (u *user) IsNil() bool {
	return u == nil
}

// Our MarshalerStream implementation
type StreamChan chan *user

func (s StreamChan) MarshalStream(enc *gojay.StreamEncoder) {
	select {
	case <-enc.Done():
		return
	case o := <-s:
		enc.Object(o)
	}
}

// Our main function
func main() {
	// get our websocket connection
	origin := "http://localhost/"
	url := "ws://localhost:12345/ws"
	ws, err := websocket.Dial(url, "", origin)
	if err != nil {
		log.Fatal(err)
	}
	// we borrow an encoder set stdout as the writer,
	// set the number of consumer to 10
	// and tell the encoder to separate each encoded element
	// added to the channel by a new line character
	enc := gojay.Stream.BorrowEncoder(ws).NConsumer(10).LineDelimited()
	// instantiate our MarshalerStream
	s := StreamChan(make(chan *user))
	// start the stream encoder
	// will block its goroutine until enc.Cancel(error) is called
	// or until something is written to the channel
	go enc.EncodeStream(s)
	// write to our MarshalerStream
	for i := 0; i < 1000; i++ {
		s <- &user{i, "username", "user@email.com"}
	}
	// Wait
	<-enc.Done()
}

Unsafe API

Unsafe API has the same functions than the regular API, it only has Unmarshal API for now. It is unsafe because it makes assumptions on the quality of the given JSON.

If you are not sure if your JSON is valid, don't use the Unsafe API.

Also, the Unsafe API does not copy the buffer when using Unmarshal API, which, in case of string decoding, can lead to data corruption if a byte buffer is reused. Using the Decode API makes Unsafe API safer as the io.Reader relies on copy builtin method and Decoder will have its own internal buffer :)

Access the Unsafe API this way:

gojay.Unsafe.Unmarshal(b, v)

Benchmarks

Benchmarks encode and decode three different data based on size (small, medium, large).

To run benchmark for decoder:

cd $GOPATH/src/github.com/francoispqt/gojay/benchmarks/decoder && make bench

To run benchmark for encoder:

cd $GOPATH/src/github.com/francoispqt/gojay/benchmarks/encoder && make bench

Benchmark Results

Decode

Small Payload

benchmark code is here

benchmark data is here

ns/opbytes/opallocs/op
Std Library25474964
JsonIter204631212
JsonParser140800
EasyJson9292402
GoJay8072562
GoJay-unsafe7121121

Medium Payload

benchmark code is here

benchmark data is here

ns/opbytes/opallocs/op
Std Library301482152496
JsonIter16309297680
JsonParser779300
EasyJson79572326
GoJay498424488
GoJay-unsafe48091447

Large Payload

benchmark code is here

benchmark data is here

ns/opbytes/opallocs/op
JsonIter210078417121136
EasyJson1066261602
JsonParser6681300
GoJay521533124177
GoJay-unsafe48277256176

Encode

Small Struct

benchmark code is here

benchmark data is here

ns/opbytes/opallocs/op
Std Library12804643
EasyJson8719446
JsonIter8662723
GoJay5431121
GoJay-func34700

Medium Struct

benchmark code is here

benchmark data is here

ns/opbytes/opallocs/op
Std Library5006149625
JsonIter2232154420
EasyJson1997154419
GoJay152231214

Large Struct

benchmark code is here

benchmark data is here

ns/opbytes/opallocs/op
Std Library6644120576332
JsonIter3524720255328
EasyJson3205315474327
GoJay278479802318

Contributing

Contributions are welcome :)

If you encounter issues please report it in Github and/or send an email at francois@parquet.ninja