package
0.4.1
Repository: https://github.com/segmentio/encoding.git
Documentation: pkg.go.dev

# README

encoding/json GoDoc

Go package offering a replacement implementation of the standard library's encoding/json package, with much better performance.

Usage

The exported API of this package mirrors the standard library's encoding/json package, the only change needed to take advantage of the performance improvements is the import path of the json package, from:

import (
    "encoding/json"
)

to

import (
    "github.com/segmentio/encoding/json"
)

One way to gain higher encoding throughput is to disable HTML escaping. It allows the string encoding to use a much more efficient code path which does not require parsing UTF-8 runes most of the time.

Performance Improvements

The internal implementation uses a fair amount of unsafe operations (untyped code, pointer arithmetic, etc...) to avoid using reflection as much as possible, which is often the reason why serialization code has a large CPU and memory footprint.

The package aims for zero unnecessary dynamic memory allocations and hot code paths that are mostly free from calls into the reflect package.

Compatibility with encoding/json

This package aims to be a drop-in replacement, therefore it is tested to behave exactly like the standard library's package. However, there are still a few missing features that have not been ported yet:

  • Streaming decoder, currently the Decoder implementation offered by the package does not support progressively reading values from a JSON array (unlike the standard library). In our experience this is a very rare use-case, if you need it you're better off sticking to the standard library, or spend a bit of time implementing it in here ;)

Note that none of those features should result in performance degradations if they were implemented in the package, and we welcome contributions!

Trade-offs

As one would expect, we had to make a couple of trade-offs to achieve greater performance than the standard library, but there were also features that we did not want to give away.

Other open-source packages offering a reduced CPU and memory footprint usually do so by designing a different API, or require code generation (therefore adding complexity to the build process). These were not acceptable conditions for us, as we were not willing to trade off developer productivity for better runtime performance. To achieve this, we chose to exactly replicate the standard library interfaces and behavior, which meant the package implementation was the only area that we were able to work with. The internals of this package make heavy use of unsafe pointer arithmetics and other performance optimizations, and therefore are not as approachable as typical Go programs. Basically, we put a bigger burden on maintainers to achieve better runtime cost without sacrificing developer productivity.

For these reasons, we also don't believe that this code should be ported upstream to the standard encoding/json package. The standard library has to remain readable and approachable to maximize stability and maintainability, and make projects like this one possible because a high quality reference implementation already exists.

# Packages

No description provided by the author

# Functions

Append acts like Marshal but appends the json representation to b instead of always reallocating a new slice.
AppendEscape appends s to b with the string escaped as a JSON value.
AppendUnescape appends s to b with the string unescaped as a JSON value.
Compact is documented at https://golang.org/pkg/encoding/json/#Compact.
Escape is a convenience helper to construct an escaped JSON string from s.
HTMLEscape is documented at https://golang.org/pkg/encoding/json/#HTMLEscape.
Indent is documented at https://golang.org/pkg/encoding/json/#Indent.
Marshal is documented at https://golang.org/pkg/encoding/json/#Marshal.
MarshalIndent is documented at https://golang.org/pkg/encoding/json/#MarshalIndent.
NewDecoder is documented at https://golang.org/pkg/encoding/json/#NewDecoder.
NewEncoder is documented at https://golang.org/pkg/encoding/json/#NewEncoder.
NewTokenizer constructs a new Tokenizer which reads its json input from b.
Parse behaves like Unmarshal but the caller can pass a set of flags to configure the parsing behavior.
Unescape is a convenience helper to unescape a JSON value.
Unmarshal is documented at https://golang.org/pkg/encoding/json/#Unmarshal.
Valid is documented at https://golang.org/pkg/encoding/json/#Valid.

# Constants

Equivalent to Delim == '['.
Bit two is set to 1, means it's a boolean.
DisallowUnknownFields is a parsing flag used to prevent decoding of objects to Go struct values when a field of the input does not match with any of the struct fields.
DontCopyNumber is a parsing flag used to provide zero-copy support when loading Number values (see DontCopyString and DontCopyRawMessage).
DontCopyRawMessage is a parsing flag used to provide zero-copy support when loading RawMessage values from a json payload.
DontCopyString is a parsing flag used to provide zero-copy support when loading string values from a json payload.
DontMatchCaseInsensitiveStructFields is a parsing flag used to prevent matching fields in a case-insensitive way.
EscapeHTML is a formatting flag used to to escape HTML in json strings.
Bool + 0.
Num + 3.
Num + 2.
Null is not zero, so we keep zero for "undefined".
Bit three is set to 1, means it's a number.
Equivalent to Delim == '{'.
SortMapKeys is formatting flag used to enable sorting of map keys when encoding JSON (this matches the behavior of the standard encoding/json package).
Bit four is set to 1, means it's a string.
Bool + 1.
TrustRawMessage is a performance optimization flag to skip value checking of raw messages.
Num + 1.
No description provided by the author
String + 1.
Decode integers into *big.Int.
Decode in-range integers to int64.
UseNumber is a parsing flag used to load numeric values as Number instead of float64.
Decode in-range positive integers to uint64.
ZeroCopy is a parsing flag that combines all the copy optimizations available in the package.

# Structs

Decoder is documented at https://golang.org/pkg/encoding/json/#Decoder.
Encoder is documented at https://golang.org/pkg/encoding/json/#Encoder.
Tokenizer is an iterator-style type which can be used to progressively parse through a json input.

# Type aliases

AppendFlags is a type used to represent configuration options that can be applied when formatting json output.
Delim is documented at https://golang.org/pkg/encoding/json/#Delim.
InvalidUnmarshalError is documented at https://golang.org/pkg/encoding/json/#InvalidUnmarshalError.
InvalidUTF8Error is documented at https://golang.org/pkg/encoding/json/#InvalidUTF8Error.
Kind represents the different kinds of value that exist in JSON.
Marshaler is documented at https://golang.org/pkg/encoding/json/#Marshaler.
MarshalerError is documented at https://golang.org/pkg/encoding/json/#MarshalerError.
Number is documented at https://golang.org/pkg/encoding/json/#Number.
ParseFlags is a type used to represent configuration options that can be applied when parsing json input.
RawMessage is documented at https://golang.org/pkg/encoding/json/#RawMessage.
RawValue represents a raw json value, it is intended to carry null, true, false, number, and string values only.
A SyntaxError is a description of a JSON syntax error.
Token is documented at https://golang.org/pkg/encoding/json/#Token.
Unmarshaler is documented at https://golang.org/pkg/encoding/json/#Unmarshaler.
UnmarshalFieldError is documented at https://golang.org/pkg/encoding/json/#UnmarshalFieldError.
UnmarshalTypeError is documented at https://golang.org/pkg/encoding/json/#UnmarshalTypeError.
UnsupportedTypeError is documented at https://golang.org/pkg/encoding/json/#UnsupportedTypeError.
UnsupportedValueError is documented at https://golang.org/pkg/encoding/json/#UnsupportedValueError.