Documentation
¶
Overview ¶
Package json registers the JSON codec with the SDK's codec registry — and no other codec — when it is imported (ADR 0134):
import _ "github.com/kitsunium/sdk/pkg/v1/data/codec/json"
Everything that dispatches through the registry by format name then reads and writes JSON: config.FileSource and config.FSSource, i18n.LoadFS, and the codec package's Marshal and Unmarshal. Importing github.com/kitsunium/sdk/pkg/v1/data/codec instead registers every format the SDK ships — BSON, CBOR, MessagePack and the rest — which a program that only reads JSON does not need to link. This package links the JSON codec, the core package declaring its codes, the standard library's encoding/json and internal/kernel/jsonenc (the standard library again), and nothing else.
A time.Duration is the integer of its nanoseconds, as encoding/json writes it: the codec's contract is the standard library's own output (#1126).
Importing both packages is harmless: a format is registered by the package that implements it, which Go initialises once however many packages import it.
Appenders ¶
For a program that writes JSON by hand, the package re-exports the appenders the SDK's own emitters use (ADR 0186 §D10):
out := json.BeginObject(buf[:0]).
String("level", "info").
Int("n", 7).
End()
stringOut := json.AppendString(nil, "a \"quoted\" word")
A string is escaped as encoding/json escapes it — U+2028 and U+2029 included, an invalid UTF-8 byte written as U+FFFD — with one difference: '<', '>' and '&' are NOT escaped, so the literal is not safe to embed in HTML as it is. The appenders register no codec and read no value by reflection, and Marshal is unchanged.
Errors ¶
Failures carry the range 0.3.2.*; match them with errs.HasCode, or the sentinels with errors.Is:
CodeMarshalFailed 0.3.2.1 encoding/json could not encode the value CodeUnmarshalFailed 0.3.2.2 the input is not JSON the target can hold CodeSizeExceeded 0.3.2.3 a value longer than NewBoundedDecoder's byte ceiling
Index ¶
Constants ¶
const ( // CodeMarshalFailed identifies a value encoding/json could not encode (0.3.2.1). CodeMarshalFailed errs.Code = corejson.CodeJSONMarshalFailed // CodeUnmarshalFailed identifies input that is not JSON the target can hold (0.3.2.2). CodeUnmarshalFailed errs.Code = corejson.CodeJSONUnmarshalFailed // CodeSizeExceeded is 0.3.2.3: a value longer than NewBoundedDecoder's byte ceiling (#891). CodeSizeExceeded errs.Code = corejson.CodeJSONSizeExceeded )
The error codes, range 0.3.2.*, declared in the core (ADR 0160).
const Format = "json"
Format is the name JSON is registered under. It is an untyped constant, so it goes wherever a format name is taken — config.FSSource's string, i18n.LoadFS's codec.Format — without a conversion.
Variables ¶
var ( // MarshalFailed is the sentinel of [CodeMarshalFailed]. MarshalFailed = corejson.MarshalFailed // UnmarshalFailed is the sentinel of [CodeUnmarshalFailed]. UnmarshalFailed = corejson.UnmarshalFailed // SizeExceeded is the sentinel of [CodeSizeExceeded]. SizeExceeded = corejson.SizeExceeded )
The sentinels, for errors.Is: each carries the code of the same name.
Functions ¶
func AppendEscaped ¶ added in v0.11.2
AppendEscaped appends the body of the JSON string literal of s, without its quotes, so a caller can join several pieces into one literal.
func AppendFloat ¶ added in v0.12.0
AppendFloat appends f to dst as the JSON number encoding/json writes for a float64 and reports true; for NaN and ±Inf it appends nothing and reports false.
func AppendString ¶ added in v0.11.2
AppendString appends s to dst as a JSON string literal, quotes included. An invalid UTF-8 byte becomes U+FFFD, U+2028 and U+2029 are escaped, and '<', '>' and '&' are NOT: the literal is not safe to embed in HTML.
func AppendUint ¶ added in v0.11.2
AppendUint appends v to dst in decimal, as a JSON number.
Types ¶
type Object ¶ added in v0.11.2
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.11.2
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.