Documentation
¶
Overview ¶
Package jsonx parses JSON without losing anything a regression gate must see: key order, duplicate keys and the literal text of every number. Strings are decoded; lone UTF-16 surrogates are kept (encoded as WTF-8) so that two different inputs never decode to the same value.
Index ¶
Constants ¶
const MaxDepth = 10000
MaxDepth bounds nesting so hostile input cannot exhaust memory.
Variables ¶
This section is empty.
Functions ¶
func Layout ¶
Layout returns the distinct runs of whitespace outside strings. Two documents written by the same encoder have the same layout even when they hold different data; compact and pretty-printed output do not.
func Marshal ¶
Marshal writes v back as JSON, keeping key order, duplicates and number literals. With spaced set it adds a space after ':' and ',', which changes the bytes but not a single value.
func Valid ¶
Valid reports whether raw is one JSON document as Parse reads it, without building its values.
Types ¶
type Kind ¶
type Kind uint8
Kind is the type of a JSON value. Integers and non-integers are separate kinds, so 1 and 1.0 differ in type, as in the reference implementation. One byte: next to Bool it keeps a Value in a smaller size class.
type Style ¶
type Style struct {
Lines uint8 // line ends: 0 none, '\n', '\r' for CRLF, '?' for a mix or a lone CR
Indent uint8 // the indentation character (' ', '\t'; '?' for a mix); 0 for none
Unit int // the first indentation's width: one level
// What follows ':' and what precedes it, and what follows ',' on the
// same line, the first time: 0 nothing, ' ' one space, '?' anything else.
Colon, BeforeColon, Comma uint8
Outside uint8 // whitespace before or after the document: 0 none, else as Lines/space
}
Style is how a document is laid out, apart from what it holds: how lines end, what one level of indentation is, and the spaces around ':' and after ','. Two documents written by the same encoder have the same style, however deep they nest and however much they hold; compact and pretty-printed output, or another indentation, do not.
type SyntaxError ¶
SyntaxError reports where parsing failed.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type Value ¶
type Value struct {
Kind Kind
Bool bool
Text string // number literal text (Int, Float, Const) or decoded string (String)
Raw string // source text of a string, quotes and escapes included
Items []*Value
Members []Member
// Src is the source text of a parsed object or array (a slice of the
// document, not a copy); nil for a value made by hand.
Src []byte
}
Value is a parsed JSON value.
func Parse ¶
Parse decodes one JSON document. The grammar matches Python's json module with its defaults (strict strings, NaN/Infinity accepted, encoding detected from the first bytes), because that is the reference implementation.
func (*Value) At ¶
At finds the value at a simple JSON path: $ followed by .key, ["key"] and [index] steps ($.entry.id, $.entries[0].slug, $.users["a.b@c.d"]), the form KeyPath writes. With duplicate keys the first one counts.
func (*Value) Equal ¶
Equal reports whether two scalars are identical: same kind and, for numbers, the same literal text. Containers are never equal here; callers walk them.
func (*Value) FormatText ¶
FormatText is what an allow-list format is matched against: strings without their quotes, every other scalar as its ScalarText.
func (*Value) ScalarText ¶
ScalarText is the text of a scalar as Python's json.dumps(v, ensure_ascii=False) writes it: numbers by their literal text, strings quoted and minimally escaped. Byte-length bookkeeping in the comparison depends on exactly this form.
type Visitor ¶
type Visitor interface {
// Member starts the value of an object member, Item the value of an
// array item; End follows that value.
Member(key string)
Item(index int)
End()
// Scalar is a value that is not a container. Text is as in Value: the
// literal of a number or a constant, the decoded string; "" for null
// and booleans.
Scalar(kind Kind, text string)
}
Visitor receives the values of a document from Walk, in source order.