Documentation
¶
Overview ¶
Package jsonkit is the JSON house style of the repository: the few named option sets that every call of encoding/json/v2 takes, and the Number type that keeps a number as it was written.
encoding/json/v2 differs from the old encoding/json (v1) by default, and what a client, an SDK, another replica or a stored file can see must not change with the move. The sets are built from the options of the v2 and jsontext packages only, one reason each, and a call site uses a set instead of its own list:
- Wire: everything that leaves the process (HTTP bodies, exporter and notifier payloads, files, ids and ETags that are hashes of an encoding).
- Lenient: input from clients, SDKs, flag files and the admin API, as lenient as v1 and GO Feature Flag are.
- Strict: configuration input that rejects what it does not know, where the code was strict before.
The standard library is used as it is: calls read as jsonv2.Marshal(value, jsonkit.Wire()). A test of internal/archtest fails for a call of Marshal or Unmarshal (and their Read, Write, Encode and Decode forms) with no option.
Index ¶
- Variables
- func Indented(indent string) interface{ ... }
- func Lenient() interface{ ... }
- func Marshal(value any) ([]byte, error)
- func MarshalLine(value any, opts jsonv2.Options) ([]byte, error)
- func MarshalWireLine(value any) ([]byte, error)
- func MarshalWrite(out io.Writer, value any) error
- func Readable() interface{ ... }
- func Strict() interface{ ... }
- func Unmarshal(data []byte, out any) error
- func Valid(data []byte) bool
- func Wire() interface{ ... }
- type Number
Constants ¶
This section is empty.
Variables ¶
var ErrNotNumber = errors.New("not a JSON number")
ErrNotNumber is the error of a Number that is read from, or written as, something that is not a JSON number.
Functions ¶
func Indented ¶
Indented is Wire with the lines indented by indent, for documents that people read or review (the OpenAPI document, the schemas, the output of the command line). It has no prefix.
func Lenient ¶
Lenient is the option set of input that GO Feature Flag accepts through encoding/json (v1), and so must be accepted here with the same meaning: SDK requests, flag set and flag files, admin requests, documents of the control plane.
- MatchCaseInsensitiveNames: "Key" and "KEY" fill the field "key" (v2 matches the exact case only).
- AllowDuplicateNames: a repeated member is read, the last value wins (v2 fails).
- AllowInvalidUTF8: invalid UTF-8 in a string becomes U+FFFD (v2 fails).
- a Number is read as the literal of a JSON number.
A repeated member inside an object that is read into an any or a typed value is merged by v2 where v1 replaced it, and no option changes that.
func MarshalLine ¶
MarshalLine is jsonv2.Marshal with a newline after the value, the form that the Encode of encoding/json (v1) wrote and that clients of the relay were served: one document, one line, in a single piece (a value that cannot be encoded leaves nothing behind).
func MarshalWireLine ¶
MarshalWireLine is MarshalLine with the options of Wire, passed as arguments like those of Marshal.
func MarshalWrite ¶
MarshalWrite encodes value as Wire does, into out.
func Readable ¶
Readable is Wire without the escaping of <, > and &, for text that people read (a change log, a redaction comparison). U+2028 and U+2029 stay escaped, as v1 did when HTML escaping was off.
func Strict ¶
Strict is Lenient that also rejects a member that no field names (RejectUnknownMembers): the configuration input that was strict before (the DisallowUnknownFields of v1).
func Valid ¶
Valid reports whether data is one JSON value, as the Valid of encoding/json (v1) does: the syntax is checked, a repeated member name and invalid UTF-8 are not refused.
func Wire ¶
Wire is the option set of every encoding that a client, another replica, a file or a hash can see. It gives the bytes that encoding/json (v1) gave, so that a body, an ETag or a state id does not change:
- Deterministic: map members are sorted by key (v1 sorted them; v2 does not by default), so that the same value always has the same bytes, which ETags and signed webhook bodies rely on.
- FormatNilSliceAsNull and FormatNilMapAsNull: a nil slice or map is null, not [] or {} (v2 default).
- EscapeForHTML and EscapeForJS: <, > and & are written as <, > and &, and U+2028 and U+2029 as and , as v1 did.
- AllowInvalidUTF8: a string with invalid UTF-8 is written with U+FFFD instead of failing (v1 did; v2 fails).
- AllowDuplicateNames: the output of a custom marshaler is not refused for a repeated name (v1 did not check).
- PreserveRawStrings: a raw value (jsontext.Value, a stored encoding) keeps its escapes (v1 did).
- a Number is written as the literal that it holds.
Types ¶
type Number ¶
type Number string
Number is a JSON number kept as it was written: "1.0" stays "1.0", "1e3" stays "1e3", and a number beyond int64 or float64 keeps every digit. Wire writes and Lenient reads that literal unchanged. The empty Number is written as 0.
func (Number) Int64 ¶
Int64 is the number as an int64; it fails for a fraction, an exponent or a value out of range.
func (Number) MarshalJSON ¶
MarshalJSON writes the literal. The empty Number is written as 0. It is a method of the type, so that no option has to carry it: an option that carries a function makes v2 build its table of marshalers for each call that builds the option anew.
func (*Number) UnmarshalJSON ¶
UnmarshalJSON reads the literal of a JSON number. A null leaves the number as it is, as it did for a json.Number of v1.