Documentation
¶
Overview ¶
Package json wraps encoding/json as a codec.Codec implementation registered under Format("json"). Blank-importing this package is enough to make JSON resolvable via the core/data/codec registry.
The registered codec encodes and decodes with encoding/json/v2 under encoding/json's v1 options — the engine and the options Go 1.27 builds encoding/json with, named so the codec states the semantics it keeps: its bytes are encoding/json's, pinned by golden files. NewV2 returns an unregistered codec under json/v2's own semantics, for a wire contract that starts with them.
It also re-exports the JSON appenders of internal/kernel/jsonenc — AppendString, AppendEscaped, AppendInt, AppendUint, AppendFloat, BeginObject and the Object it returns — for the programs that write a JSON document by hand and want the escaping the SDK's own emitters use (ADR 0186 §D10). They register no codec and read no value by reflection; Marshal keeps encoding/json's semantics. A string is escaped as encoding/json escapes it, except that '<', '>' and '&' are NOT.
Package json — adapts *encoding/json.Decoder to codec.Decoder.
Package json — adapts *encoding/json.Encoder to codec.Encoder.
Index ¶
- Variables
- func AppendEscaped(dst []byte, s string) []byte
- func AppendFloat(dst []byte, f float64) (number []byte, ok bool)
- func AppendInt(dst []byte, v int64) []byte
- func AppendString(dst []byte, s string) []byte
- func AppendUint(dst []byte, v uint64) []byte
- func New() codec.Codec
- func NewV2() codec.Codec
- type Object
Constants ¶
This section is empty.
Variables ¶
var Codec codec.Codec = codec.Register(&jsonCodec{})
Codec is the JSON singleton, registered with core/data/codec at package load. Binding the registration result to a named var is more idiomatic than `var _ = codec.Register(...)` and keeps us clear of init().
Functions ¶
func AppendEscaped ¶ added in v0.26.15
AppendEscaped appends the body of the JSON string literal of s — what AppendString writes between its quotes — so a caller can join several pieces into one literal.
func AppendFloat ¶ added in v0.27.0
AppendFloat appends f to dst as the JSON number encoding/json writes for a float64 (1000000, not 1e+06; 1e-7, not 1e-07) and reports true; for NaN and ±Inf it appends nothing and reports false, so the caller writes its own policy for them.
func AppendString ¶ added in v0.26.15
AppendString appends s to dst as a JSON string literal, quotes included. See the package comment for the escapes: invalid UTF-8 becomes U+FFFD, U+2028 and U+2029 are escaped, and '<', '>' and '&' are not.
func AppendUint ¶ added in v0.26.15
AppendUint appends v to dst in decimal, as a JSON number. A value past 2^53 is exact here and is not in a JavaScript reader.
func NewV2 ¶ added in v0.27.0
NewV2 returns a fresh JSON codec under encoding/json/v2's semantics, for a wire contract that starts with them: member names matched case-sensitively, a duplicate name or invalid UTF-8 refused, a nil slice or map written as [] or {}, a byte array as base64, omitempty meaning an empty JSON value, omitzero honoured, a time.Duration refused without a format — and map members in sorted order. It is not registered: Format("json") keeps the v1 semantics every existing document was written with, and this codec is reached only through the value returned. Its errors carry the codes of the registered codec.
Types ¶
type Object ¶ added in v0.26.15
Object is a JSON object being written by BeginObject: members land in the order they are named, and nothing allocates but the caller's slice.
func BeginObject ¶ added in v0.26.15
BeginObject appends the opening brace of an object to dst and returns the Object that writes its members after it, in the order they are named.