Fast JSON serializer for golang.

Overview

easyjson Build Status Go Report Card

Package easyjson provides a fast and easy way to marshal/unmarshal Go structs to/from JSON without the use of reflection. In performance tests, easyjson outperforms the standard encoding/json package by a factor of 4-5x, and other JSON encoding packages by a factor of 2-3x.

easyjson aims to keep generated Go code simple enough so that it can be easily optimized or fixed. Another goal is to provide users with the ability to customize the generated code by providing options not available with the standard encoding/json package, such as generating "snake_case" names or enabling omitempty behavior by default.

Usage

# install
go get -u github.com/mailru/easyjson/...

# run
easyjson -all <file>.go

The above will generate <file>_easyjson.go containing the appropriate marshaler and unmarshaler funcs for all structs contained in <file>.go.

Please note that easyjson requires a full Go build environment and the GOPATH environment variable to be set. This is because easyjson code generation invokes go run on a temporary file (an approach to code generation borrowed from ffjson).

Options

Usage of easyjson:
  -all
    	generate marshaler/unmarshalers for all structs in a file
  -build_tags string
        build tags to add to generated file
  -gen_build_flags string
        build flags when running the generator while bootstrapping
  -byte
        use simple bytes instead of Base64Bytes for slice of bytes
  -leave_temps
    	do not delete temporary files
  -no_std_marshalers
    	don't generate MarshalJSON/UnmarshalJSON funcs
  -noformat
    	do not run 'gofmt -w' on output file
  -omit_empty
    	omit empty fields by default
  -output_filename string
    	specify the filename of the output
  -pkg
    	process the whole package instead of just the given file
  -snake_case
    	use snake_case names instead of CamelCase by default
  -lower_camel_case
        use lowerCamelCase instead of CamelCase by default
  -stubs
    	only generate stubs for marshaler/unmarshaler funcs
  -disallow_unknown_fields
        return error if some unknown field in json appeared
  -disable_members_unescape
        disable unescaping of \uXXXX string sequences in member names

Using -all will generate marshalers/unmarshalers for all Go structs in the file excluding those structs whose preceding comment starts with easyjson:skip. For example:

//easyjson:skip
type A struct {}

If -all is not provided, then only those structs whose preceding comment starts with easyjson:json will have marshalers/unmarshalers generated. For example:

//easyjson:json
type A struct {}

Additional option notes:

  • -snake_case tells easyjson to generate snake_case field names by default (unless overridden by a field tag). The CamelCase to snake_case conversion algorithm should work in most cases (ie, HTTPVersion will be converted to "http_version").

  • -build_tags will add the specified build tags to generated Go sources.

  • -gen_build_flags will execute the easyjson bootstapping code to launch the actual generator command with provided flags. Multiple arguments should be separated by space e.g. -gen_build_flags="-mod=mod -x".

Structure json tag options

Besides standart json tag options like 'omitempty' the following are supported:

  • 'nocopy' - disables allocation and copying of string values, making them refer to original json buffer memory. This works great for short lived objects which are not hold in memory after decoding and immediate usage. Note if string requires unescaping it will be processed as normally.
  • 'intern' - string "interning" (deduplication) to save memory when the very same string dictionary values are often met all over the structure. See below for more details.

Generated Marshaler/Unmarshaler Funcs

For Go struct types, easyjson generates the funcs MarshalEasyJSON / UnmarshalEasyJSON for marshaling/unmarshaling JSON. In turn, these satisfy the easyjson.Marshaler and easyjson.Unmarshaler interfaces and when used in conjunction with easyjson.Marshal / easyjson.Unmarshal avoid unnecessary reflection / type assertions during marshaling/unmarshaling to/from JSON for Go structs.

easyjson also generates MarshalJSON and UnmarshalJSON funcs for Go struct types compatible with the standard json.Marshaler and json.Unmarshaler interfaces. Please be aware that using the standard json.Marshal / json.Unmarshal for marshaling/unmarshaling will incur a significant performance penalty when compared to using easyjson.Marshal / easyjson.Unmarshal.

Additionally, easyjson exposes utility funcs that use the MarshalEasyJSON and UnmarshalEasyJSON for marshaling/unmarshaling to and from standard readers and writers. For example, easyjson provides easyjson.MarshalToHTTPResponseWriter which marshals to the standard http.ResponseWriter. Please see the GoDoc listing for the full listing of utility funcs that are available.

Controlling easyjson Marshaling and Unmarshaling Behavior

Go types can provide their own MarshalEasyJSON and UnmarshalEasyJSON funcs that satisfy the easyjson.Marshaler / easyjson.Unmarshaler interfaces. These will be used by easyjson.Marshal and easyjson.Unmarshal when defined for a Go type.

Go types can also satisfy the easyjson.Optional interface, which allows the type to define its own omitempty logic.

Type Wrappers

easyjson provides additional type wrappers defined in the easyjson/opt package. These wrap the standard Go primitives and in turn satisfy the easyjson interfaces.

The easyjson/opt type wrappers are useful when needing to distinguish between a missing value and/or when needing to specifying a default value. Type wrappers allow easyjson to avoid additional pointers and heap allocations and can significantly increase performance when used properly.

Memory Pooling

easyjson uses a buffer pool that allocates data in increasing chunks from 128 to 32768 bytes. Chunks of 512 bytes and larger will be reused with the help of sync.Pool. The maximum size of a chunk is bounded to reduce redundant memory allocation and to allow larger reusable buffers.

easyjson's custom allocation buffer pool is defined in the easyjson/buffer package, and the default behavior pool behavior can be modified (if necessary) through a call to buffer.Init() prior to any marshaling or unmarshaling. Please see the GoDoc listing for more information.

String interning

During unmarshaling, string field values can be optionally interned to reduce memory allocations and usage by deduplicating strings in memory, at the expense of slightly increased CPU usage.

This will work effectively only for string fields being decoded that have frequently the same value (e.g. if you have a string field that can only assume a small number of possible values).

To enable string interning, add the intern keyword tag to your json tag on string fields, e.g.:

type Foo struct {
  UUID  string `json:"uuid"`         // will not be interned during unmarshaling
  State string `json:"state,intern"` // will be interned during unmarshaling
}

Issues, Notes, and Limitations

  • easyjson is still early in its development. As such, there are likely to be bugs and missing features when compared to encoding/json. In the case of a missing feature or bug, please create a GitHub issue. Pull requests are welcome!

  • Unlike encoding/json, object keys are case-sensitive. Case-insensitive matching is not currently provided due to the significant performance hit when doing case-insensitive key matching. In the future, case-insensitive object key matching may be provided via an option to the generator.

  • easyjson makes use of unsafe, which simplifies the code and provides significant performance benefits by allowing no-copy conversion from []byte to string. That said, unsafe is used only when unmarshaling and parsing JSON, and any unsafe operations / memory allocations done will be safely deallocated by easyjson. Set the build tag easyjson_nounsafe to compile it without unsafe.

  • easyjson is compatible with Google App Engine. The appengine build tag (set by App Engine's environment) will automatically disable the use of unsafe, which is not allowed in App Engine's Standard Environment. Note that the use with App Engine is still experimental.

  • Floats are formatted using the default precision from Go's strconv package. As such, easyjson will not correctly handle high precision floats when marshaling/unmarshaling JSON. Note, however, that there are very few/limited uses where this behavior is not sufficient for general use. That said, a different package may be needed if precise marshaling/unmarshaling of high precision floats to/from JSON is required.

  • While unmarshaling, the JSON parser does the minimal amount of work needed to skip over unmatching parens, and as such full validation is not done for the entire JSON value being unmarshaled/parsed.

  • Currently there is no true streaming support for encoding/decoding as typically for many uses/protocols the final, marshaled length of the JSON needs to be known prior to sending the data. Currently this is not possible with easyjson's architecture.

  • easyjson parser and codegen based on reflection, so it won't work on package main files, because they cant be imported by parser.

Benchmarks

Most benchmarks were done using the example 13kB example JSON (9k after eliminating whitespace). This example is similar to real-world data, is well-structured, and contains a healthy variety of different types, making it ideal for JSON serialization benchmarks.

Note:

  • For small request benchmarks, an 80 byte portion of the above example was used.

  • For large request marshaling benchmarks, a struct containing 50 regular samples was used, making a ~500kB output JSON.

  • Benchmarks are showing the results of easyjson's default behaviour, which makes use of unsafe.

Benchmarks are available in the repository and can be run by invoking make.

easyjson vs. encoding/json

easyjson is roughly 5-6 times faster than the standard encoding/json for unmarshaling, and 3-4 times faster for non-concurrent marshaling. Concurrent marshaling is 6-7x faster if marshaling to a writer.

easyjson vs. ffjson

easyjson uses the same approach for JSON marshaling as ffjson, but takes a significantly different approach to lexing and parsing JSON during unmarshaling. This means easyjson is roughly 2-3x faster for unmarshaling and 1.5-2x faster for non-concurrent unmarshaling.

As of this writing, ffjson seems to have issues when used concurrently: specifically, large request pooling hurts ffjson's performance and causes scalability issues. These issues with ffjson can likely be fixed, but as of writing remain outstanding/known issues with ffjson.

easyjson and ffjson have similar performance for small requests, however easyjson outperforms ffjson by roughly 2-5x times for large requests when used with a writer.

easyjson vs. go/codec

go/codec provides compile-time helpers for JSON generation. In this case, helpers do not work like marshalers as they are encoding-independent.

easyjson is generally 2x faster than go/codec for non-concurrent benchmarks and about 3x faster for concurrent encoding (without marshaling to a writer).

In an attempt to measure marshaling performance of go/codec (as opposed to allocations/memcpy/writer interface invocations), a benchmark was done with resetting length of a byte slice rather than resetting the whole slice to nil. However, the optimization in this exact form may not be applicable in practice, since the memory is not freed between marshaling operations.

easyjson vs 'ujson' python module

ujson is using C code for parsing, so it is interesting to see how plain golang compares to that. It is important to note that the resulting object for python is slower to access, since the library parses JSON object into dictionaries.

easyjson is slightly faster for unmarshaling and 2-3x faster than ujson for marshaling.

Benchmark Results

ffjson results are from February 4th, 2016, using the latest ffjson and go1.6. go/codec results are from March 4th, 2016, using the latest go/codec and go1.6.

Unmarshaling

lib json size MB/s allocs/op B/op
standard regular 22 218 10229
standard small 9.7 14 720
easyjson regular 125 128 9794
easyjson small 67 3 128
ffjson regular 66 141 9985
ffjson small 17.6 10 488
codec regular 55 434 19299
codec small 29 7 336
ujson regular 103 N/A N/A

Marshaling, one goroutine.

lib json size MB/s allocs/op B/op
standard regular 75 9 23256
standard small 32 3 328
standard large 80 17 1.2M
easyjson regular 213 9 10260
easyjson* regular 263 8 742
easyjson small 125 1 128
easyjson large 212 33 490k
easyjson* large 262 25 2879
ffjson regular 122 153 21340
ffjson** regular 146 152 4897
ffjson small 36 5 384
ffjson** small 64 4 128
ffjson large 134 7317 818k
ffjson** large 125 7320 827k
codec regular 80 17 33601
codec*** regular 108 9 1153
codec small 42 3 304
codec*** small 56 1 48
codec large 73 483 2.5M
codec*** large 103 451 66007
ujson regular 92 N/A N/A

* marshaling to a writer, ** using ffjson.Pool(), *** reusing output slice instead of resetting it to nil

Marshaling, concurrent.

lib json size MB/s allocs/op B/op
standard regular 252 9 23257
standard small 124 3 328
standard large 289 17 1.2M
easyjson regular 792 9 10597
easyjson* regular 1748 8 779
easyjson small 333 1 128
easyjson large 718 36 548k
easyjson* large 2134 25 4957
ffjson regular 301 153 21629
ffjson** regular 707 152 5148
ffjson small 62 5 384
ffjson** small 282 4 128
ffjson large 438 7330 1.0M
ffjson** large 131 7319 820k
codec regular 183 17 33603
codec*** regular 671 9 1157
codec small 147 3 304
codec*** small 299 1 48
codec large 190 483 2.5M
codec*** large 752 451 77574

* marshaling to a writer, ** using ffjson.Pool(), *** reusing output slice instead of resetting it to nil

Issues
  • Fix unmarshaling struct with interface field which is a pointer to another struct

    Fix unmarshaling struct with interface field which is a pointer to another struct

    This pull request fixes a bug connected with unmarshaling struct which contains interface field, where interface is a pointer to another struct. Instead of filled embedded struct we get map[string]interface{}. See TestUnmarshalStructWithEmbeddedPtrStruct for example.

    opened by doroginin 13
  • Support easyjson.Marshaler as field type

    Support easyjson.Marshaler as field type

    I have struct

    	type SocketCommand struct {
    		CMD   string      `json:"cmd"`
    		Value interface{} `json:"cmdval"`
    	}
    

    and auto generated Marshaler

    func easyjsonCe9b818aEncodeEngine(out *jwriter.Writer, in SocketCommand) {
    // ...
    		if m, ok := in.Value.(easyjson.Marshaler); ok {
    			m.MarshalEasyJSON(out)
    		} else if m, ok := in.Value.(json.Marshaler); ok {
    			out.Raw(m.MarshalJSON())
    		} else {
    			out.Raw(json.Marshal(in.Value))
    		}
    // ...
    }
    

    So i see some overhead m, ok := in.Value.(easyjson.Marshaler); ok, and think, how about change struct to another more optimize

    	OptimizeSocketCommand struct {
    		CMD   string             `json:"cmd"`
    		Value easyjson.Marshaler `json:"cmdval"`
    	}
    

    after try

    easyjson -all ./structs.go
    

    i see:

    interface type easyjson.Marshaler not supported: only interface{} is allowed
    exit status 1
    Bootstrap failed: exit status 1
    

    But expect success

    Proposal: Add support for:

    type Marshaler interface {
    	MarshalEasyJSON(w *jwriter.Writer)
    }
    
    // Marshaler is an easyjson-compatible unmarshaler interface.
    type Unmarshaler interface {
    	UnmarshalEasyJSON(w *jlexer.Lexer)
    }
    

    and some combine type what cover Marshaler & Unmarshaler together. See @121, but current issue is other, and expect

    func easyjsonCe9b818aEncodeEngine(out *jwriter.Writer, in SocketCommand) {
    // ...
    			in.Value.MarshalEasyJSON(out)
    // ...
    }
    
    enhancement 
    opened by ReenExe 12
  • Package is a program, not an importable package

    Package is a program, not an importable package

    Fails when running against this file...

    I've created package foo, and have the following file main.go

    package main
    
    import (
        "fmt"
    )
    
    type Foo struct {
        Name string
    }
    
    func main() {
        f := Foo{Name: "Bar"}
        fmt.Printf("%+v\n", f)
    }
    

    Output

    $ easyjson -all main.go 
    /var/folders/wj/ggsd2sgs2b1_vnplhkcv6g0jk5xz5m/T/easyjson715726581.go:8:3: import "github.com/scottjbarr/foo" is a program, not an importable package
    Bootstrap failed: exit status 1
    

    A file called main_easyjson.go with the following content is created

    package  main
    
    import (
      "github.com/mailru/easyjson/jwriter"
      "github.com/mailru/easyjson/jlexer"
    )
    func (* Foo ) MarshalJSON() ([]byte, error) { return nil, nil }
    func (* Foo ) UnmarshalJSON([]byte) error { return nil }
    func (* Foo ) MarshalEasyJSON(w *jwriter.Writer) {}
    func (* Foo ) UnmarshalEasyJSON(l *jlexer.Lexer) {}
    
    • Also, the output file is not gofmt'ed.
    not_in_plan 
    opened by scottjbarr 12
  • easyjson unmarshals empty slice `[]` into nil slice instead of empty slice, which in turn will be marshalled to `null` instead of empty slice.

    easyjson unmarshals empty slice `[]` into nil slice instead of empty slice, which in turn will be marshalled to `null` instead of empty slice.

    Hi, I've noticed some strange behaviour recently. See below for the details.

    First of all, I define the following type:

    type Attributes map[string]interface{}
    

    This allows me to store a key-value set with the value being anything I like.

    I then give this some values like this:

    var attributes = Attributes{
        "key1": "value1",
        "key2": 2,
        "key3": []string{"v31", "v32"},
       "key4":  []string{},
    }
    

    If I marshal this into JSON, it looks like:

    bytes, _ = easyjson.Marshal(attributes)
    fmt.Println("Marshaled JSON:", string(bytes))
    
    output json:
    {
      "key1": "value1",
      "key2": 2,
      "key3": [
        "v31",
        "v32"
      ],
      "key4": []
    }
    

    If I store this JSON string somewhere, e.g. database and use it later. When I read it out and unmarshal it back to Attributes, it looks like this:

    easyjson.Unmarshal(bytes, &attributes)
    fmt.Println("easyjson marshalled attributes:", attributes)
    fmt.Println("attributes.key4 is nil?", attributes["key4"].([]interface{}) == nil)
    
    output:
    easyjson marshalled attributes: map[key4:[] key1:value1 key2:2 key3:[v31 v32]]
    attributes.key4 is nil? true
    

    Now the value of key4 becomes nil, instead of empty slice. This is problematic since if I marshal it again, it becomes "null" in the JSON:

    bytes, _ = easyjson.Marshal(attributes)
    fmt.Println("Marshaled JSON Again:", string(bytes))
    
    output:
    Marshaled JSON Again: {"key2":2,"key3":["v31","v32"],"key4":null,"key1":"value1"}
    

    If I use encoding/json library instead of easyjson, it works ok. See below:

    bytes, _ = json.Marshal(attributes)
    fmt.Println("Marshaled JSON:", string(bytes))
    
    json.Unmarshal(bytes, &attributes)
    fmt.Println("easyjson marshalled attributes:", attributes)
    fmt.Println("attributes.key4 is nil?", attributes["key4"].([]interface{}) == nil)
    
    bytes, _ = json.Marshal(attributes)
    fmt.Println("Marshaled JSON Again:", string(bytes))
    

    output:

    {"type":"Feature","properties":{"name":"Connexis","address":"1 Fusionopolis Way"},"geometry":{"type":"Point","coordinates":[1.29878,103.787668]}}
    Marshaled JSON: {"key1":"value1","key2":2,"key3":["v31","v32"],"key4":[]}
    easyjson marshalled attributes: map[key4:[] key1:value1 key2:2 key3:[v31 v32]]
    attributes.key4 is nil? false
    Marshaled JSON Again: {"key1":"value1","key2":2,"key3":["v31","v32"],"key4":[]}
    

    It's consistent, empty slice is always empty slice, not nil, and null.

    opened by WUMUXIAN 11
  • Marshal custom map keys

    Marshal custom map keys

    Hi, if I try to marshal struct like this

    type Flag int m := map[Flag]bool easyjson.Marshal(&m)

    map key of type Flag convert to integer in string, but if it implement TextMarshaler interface standart library convert it to string value returns from MarshalText method, would be nice to maintain this behavoir

    opened by LepikovStan 10
  • easyjson does not generate code in Go 1.15 environment

    easyjson does not generate code in Go 1.15 environment

    When running easyjson without -all option under Go 1.15, easyjson outputs only import and var headers, but no JSON (un)marshalling methods. My guess is that something prevents it to see the //easyjson:json comments.

    To reproduce it should be enough to run make with Go 1.15.

    bug 
    opened by martin-sucha 9
  • easyjson does not respect setting escape-html to false

    easyjson does not respect setting escape-html to false

    type Stuff struct {
    	Thing string
    }
    v := &Stuff{Thing: `hello <&,.='"'=.,&> world`}
    
    buffer := &bytes.Buffer{}
    encoder := json.NewEncoder(buffer)
    encoder.SetEscapeHTML(false)
    _ = encoder.Encode(v)
    

    The above will give the correct {"Thing":"hello <&,.='\"'=.,&> world"} when not using easyjson. With easyjson, you get: {"Thing":"hello \u003c\u0026,.='\"'=.,\u0026\u003e world"}

    opened by veqryn 9
  • Generate CamelCase function names instead of under_score to pass golint check

    Generate CamelCase function names instead of under_score to pass golint check

    Generate CamelCase function names instead of under_score according to default Go naming convention.

    I changed also some generated variable names and added generated comment before exported functions.

    The goal of changes is to generate the code which will pass default golint/gohint check without adding easyjson-generated files to ignore.

    opened by vpbarb 9
  • Implement optional string interning

    Implement optional string interning

    Uses josharian/intern to optionally perform string interning during unmarshaling. Saves one allocation per string field.

    Fixes https://github.com/mailru/easyjson/issues/191

    opened by CAFxX 8
  • Optimisations: use bytes.IndexByte() + delay unescaping (#264)

    Optimisations: use bytes.IndexByte() + delay unescaping (#264)

    There are 2 issues with current implementation (addresses issue #264 ):

    1. It performs a plain byte to byte loop to find string boundaries and perform unescaping. Replace this with bytes.IndexByte() implementation.

    2. It performs unescaping of string values even when this is not really needed, e.g. for members which are absent in target data structure.

    This patch fixes both issues and results in ~12% faster BenchmarkEJ_Unmarshal_M-8, plus number of allocations goes down from 128 to 52 (!):

    benchmark                                     old MB/s     new MB/s     speedup
    BenchmarkEJ_Unmarshal_M-8                     317.99       356.27       1.12x
    BenchmarkEJ_Unmarshal_S-8                     142.19       139.77       0.98x
    
    benchmark                                     old allocs     new allocs     delta
    BenchmarkEJ_Unmarshal_M-8                     128            52             -59.38%
    BenchmarkEJ_Unmarshal_S-8                     3              3              +0.00%
    

    The rest of std benchmarks are w/o changes. The benchmark in the original issue became about 1.5x faster.

    NOTE: performance is even more improved if disable unescaping of member names, ints, base64 bytes using a new -disable_members_unescape option:

    benchmark                                     old MB/s     new MB/s     speedup
    BenchmarkEJ_Unmarshal_M-8                     317.99       406.93       1.28x
    BenchmarkEJ_Unmarshal_S-8                     142.19       150.85       1.06x
    
    opened by kirillx 8
  • Optimize generated encoders

    Optimize generated encoders

    Fixes https://github.com/mailru/easyjson/issues/136

    This changes the generated code for a omitempty field from:

    	if in.Name != nil {
    		if !first {
    			out.RawByte(',')
    		}
    		first = false
    		out.RawString("\"name\":")
    		if in.Name == nil {
    			out.RawString("null")
    		} else {
    			out.String(string(*in.Name))
    		}
    	}
    

    to:

    	if in.Name != nil {
    		if first {
    			first = false
    			out.RawString("\"name\":")
                    } else {
    			out.RawString(",\"name\":")
    		}
    		out.String(string(*in.Name))
    	}
    
    opened by CAFxX 8
  • Allow using pointer receivers, and use struct pointers for input parameters

    Allow using pointer receivers, and use struct pointers for input parameters

    This PR changes two things:

    • It adds a boolean ptr_receivers flag (default false), that, when set to true, causes generated Marshal[Easy]JSON methods to be generated with a pointer receiver instead of a value receiver.
    • It causes internal, generated marshaling functions to expect input structs to be passed as pointers instead of values.

    This PR unconditionally reverts the effect of #15 on the generated internal functions, and allows conditionally reverting the effect of #15 on the generated external (exported) methods.

    The motivation is to allow easyjson to work with the new Google protobuf API (see fixed issue).

    Fixes #348 .

    opened by misberner 1
  • Feature suggestion: add new option disallow_duplicate_fields

    Feature suggestion: add new option disallow_duplicate_fields

    This option makes unmarshalling return an error if a field appears in the json more than once.

    This option can be useful as a precaution against JSON Interoperability Vulnerabilities, see for example "Inconsistent Duplicate Key Precedence" in https://bishopfox.com/blog/json-interoperability-vulnerabilities

    Also, a similar option is planned for the Golang standard library: https://github.com/golang/go/issues/48298

    For reference, here is the generated code with the option:

    // Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
    
    package tests
    
    import (
    	json "encoding/json"
    	fmt "fmt"
    	easyjson "github.com/mailru/easyjson"
    	jlexer "github.com/mailru/easyjson/jlexer"
    	jwriter "github.com/mailru/easyjson/jwriter"
    )
    
    // suppress unused package warning
    var (
    	_ *json.RawMessage
    	_ *jlexer.Lexer
    	_ *jwriter.Writer
    	_ easyjson.Marshaler
    )
    
    func easyjson4f43fa15DecodeGithubComMailruEasyjsonTests(in *jlexer.Lexer, out *Dupl) {
    	isTopLevel := in.IsStart()
    	if in.IsNull() {
    		if isTopLevel {
    			in.Consumed()
    		}
    		in.Skip()
    		return
    	}
    	var ASet bool
    	var BSet bool
    	in.Delim('{')
    	for !in.IsDelim('}') {
    		key := in.UnsafeFieldName(false)
    		in.WantColon()
    		if in.IsNull() {
    			in.Skip()
    			in.WantComma()
    			continue
    		}
    		switch key {
    		case "a":
    			if ASet {
    				in.AddError(fmt.Errorf("duplicate field A"))
    			}
    			out.A = int(in.Int())
    			ASet = true
    		case "b":
    			if BSet {
    				in.AddError(fmt.Errorf("duplicate field B"))
    			}
    			out.B = int(in.Int())
    			BSet = true
    		default:
    			in.SkipRecursive()
    		}
    		in.WantComma()
    	}
    	in.Delim('}')
    	if isTopLevel {
    		in.Consumed()
    	}
    }
    func easyjson4f43fa15EncodeGithubComMailruEasyjsonTests(out *jwriter.Writer, in Dupl) {
    	out.RawByte('{')
    	first := true
    	_ = first
    	{
    		const prefix string = ",\"a\":"
    		out.RawString(prefix[1:])
    		out.Int(int(in.A))
    	}
    	{
    		const prefix string = ",\"b\":"
    		out.RawString(prefix)
    		out.Int(int(in.B))
    	}
    	out.RawByte('}')
    }
    
    // MarshalJSON supports json.Marshaler interface
    func (v Dupl) MarshalJSON() ([]byte, error) {
    	w := jwriter.Writer{}
    	easyjson4f43fa15EncodeGithubComMailruEasyjsonTests(&w, v)
    	return w.Buffer.BuildBytes(), w.Error
    }
    
    // MarshalEasyJSON supports easyjson.Marshaler interface
    func (v Dupl) MarshalEasyJSON(w *jwriter.Writer) {
    	easyjson4f43fa15EncodeGithubComMailruEasyjsonTests(w, v)
    }
    
    // UnmarshalJSON supports json.Unmarshaler interface
    func (v *Dupl) UnmarshalJSON(data []byte) error {
    	r := jlexer.Lexer{Data: data}
    	easyjson4f43fa15DecodeGithubComMailruEasyjsonTests(&r, v)
    	return r.Error()
    }
    
    // UnmarshalEasyJSON supports easyjson.Unmarshaler interface
    func (v *Dupl) UnmarshalEasyJSON(l *jlexer.Lexer) {
    	easyjson4f43fa15DecodeGithubComMailruEasyjsonTests(l, v)
    }
    
    opened by peterzeller 0
  • easyjson didn't generate all data. MFTI/perfomance/json example

    easyjson didn't generate all data. MFTI/perfomance/json example

    Hi. My golang version is go1.18.3 linux/amd64

    I ran the command: [email protected]:~/Go/MFTI/perfomance/json$ easyjson struct.go And got an error: # command-line-arguments main.main: relocation target type.json_test.EasyJSON_exporter_User not defined Bootstrap failed: exit status 2

    File struct_easyjson.go has only lines: // TEMPORARY AUTOGENERATED FILE: easyjson stub code to make the package // compilable during generation.

    _package main

    import ( "github.com/mailru/easyjson/jwriter" "github.com/mailru/easyjson/jlexer" )

    func ( User ) MarshalJSON() ([]byte, error) { return nil, nil } func (* User ) UnmarshalJSON([]byte) error { return nil } func ( User ) MarshalEasyJSON(w jwriter.Writer) {} func ( User ) UnmarshalEasyJSON(l *jlexer.Lexer) {}

    type EasyJSON_exporter_User *User__

    opened by RomanAvdeenko 0
  • Avoid extra allocation using buffer pools

    Avoid extra allocation using buffer pools

    Inserting directly a slice into a sync.Pool object caused the allocation for an extra object every Put call. Avoid it using unsafe.Pointer to the underlying array.

    opened by fziglio 1
Releases(v0.7.7)
Owner
Free and open source software developed at Mail.Ru
Free and open source software developed at Mail.Ru
JSON Spanner - A Go package that provides a fast and simple way to filter or transform a json document

JSON SPANNER JSON Spanner is a Go package that provides a fast and simple way to

null 4 Jul 18, 2022
Fast Color JSON Marshaller + Pretty Printer for Golang

ColorJSON: The Fast Color JSON Marshaller for Go What is this? This package is based heavily on hokaccha/go-prettyjson but has some noticible differen

Tyler Brock 112 Jul 25, 2022
Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Qntfy 205 Sep 17, 2021
Get JSON values quickly - JSON parser for Go

get json values quickly GJSON is a Go package that provides a fast and simple way to get values from a json document. It has features such as one line

Josh Baker 10.8k Aug 12, 2022
JSON diff library for Go based on RFC6902 (JSON Patch)

jsondiff jsondiff is a Go package for computing the diff between two JSON documents as a series of RFC6902 (JSON Patch) operations, which is particula

William Poussier 179 Aug 4, 2022
Package json implements encoding and decoding of JSON as defined in RFC 7159

Package json implements encoding and decoding of JSON as defined in RFC 7159. The mapping between JSON and Go values is described in the documentation for the Marshal and Unmarshal functions

High Performance, Kubernetes Native Object Storage 2 Jun 26, 2022
Json-go - CLI to convert JSON to go and vice versa

Json To Go Struct CLI Install Go version 1.17 go install github.com/samit22/js

Samit Ghimire 7 Jul 29, 2022
Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection

fastjson - fast JSON parser and validator for Go Features Fast. As usual, up to 15x faster than the standard encoding/json. See benchmarks. Parses arb

Aliaksandr Valialkin 1.6k Aug 4, 2022
Fast and flexible JSON encoder for Go

Jettison Jettison is a fast and flexible JSON encoder for the Go programming language, inspired by bet365/jingo, with a richer features set, aiming at

William Poussier 129 Jul 10, 2022
A fast json parser for go

rjson rjson is a json parser that relies on Ragel-generated state machines for most parsing. rjson's api is minimal and focussed on efficient parsing.

WillAbides 48 Jul 27, 2022
A blazingly fast JSON serializing & deserializing library

Sonic A blazingly fast JSON serializing & deserializing library, accelerated by JIT(just-in-time compiling) and SIMD(single-instruction-multi-data). B

Bytedance Inc. 3.2k Aug 5, 2022
Fast json for go. Lightweight fork of jsoniter.

jx Fast json for go. Lightweight fork of jsoniter. Features Reduced scope (no reflection or encoding/json adapter) Fuzzing, improved test coverage Dra

ogen 63 Jul 5, 2022
Abstract JSON for golang with JSONPath support

Abstract JSON Abstract JSON is a small golang package provides a parser for JSON with support of JSONPath, in case when you are not sure in its struct

Stepan Pyzhov 122 Jul 27, 2022
JSON query in Golang

gojq JSON query in Golang. Install go get -u github.com/elgs/gojq This library serves three purposes: makes parsing JSON configuration file much easie

Qian Chen 182 Apr 27, 2022
Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Aditya Mukerjee 2.5k Aug 8, 2022
Arbitrary transformations of JSON in Golang

kazaam Description Kazaam was created with the goal of supporting easy and fast transformations of JSON data with Golang. This functionality provides

Qntfy 232 Jul 16, 2022
Parsing JSON is a hassle in golang

GoJSON Parsing JSON is a hassle in golang. This package will allow you to parse and search elements in a json without structs. Install gojson go get g

swaraj18 25 Nov 12, 2021
Fastest JSON interperter for golang

Welcome To JIN "Your wish is my command" Fast and Easy Way to Deal With JSON Jin is a comprehensive JSON manipulation tool bundle. All functions teste

eco 58 May 28, 2022
Golang port of simdjson: parsing gigabytes of JSON per second

This is a Golang port of simdjson, a high performance JSON parser developed by Daniel Lemire and Geoff Langdale. It makes extensive use of SIMD instructions to achieve parsing performance of gigabytes of JSON per second.

High Performance, Kubernetes Native Object Storage 1.4k Aug 6, 2022