jsonx

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

View Source
const MaxDepth = 10000

MaxDepth bounds nesting so hostile input cannot exhaust memory.

Variables

This section is empty.

Functions

func KeyPath

func KeyPath(path, key string) string

KeyPath is the path of a member of the object at path.

func Layout

func Layout(raw []byte) map[string]bool

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

func Marshal(v *Value, spaced bool) []byte

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 Quote

func Quote(s string) string

Quote escapes s like Python's json encoder with ensure_ascii=False.

func Valid

func Valid(raw []byte) bool

Valid reports whether raw is one JSON document as Parse reads it, without building its values.

func Walk

func Walk(raw []byte, v Visitor) error

Walk reads a document as Parse does, with the same grammar and errors, but hands its values to v instead of building them: a scan of a large body then holds no tree, which takes many times the body's size. After a syntax error v has seen the values before it; the caller drops them.

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.

const (
	Null Kind = iota
	Bool
	Int
	Float
	Const // NaN, Infinity, -Infinity (accepted by the reference parser)
	String
	Array
	Object
)

func (Kind) String

func (k Kind) String() string

type Member

type Member struct {
	Key   string
	Value *Value
}

Member is one key/value pair of an object, in source order.

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.

func StyleOf

func StyleOf(raw []byte) Style

StyleOf reads the style of a document in one pass, without allocating.

type SyntaxError

type SyntaxError struct {
	Offset int
	Msg    string
}

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

func Parse(raw []byte) (*Value, error)

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

func (v *Value) At(path string) (*Value, error)

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) Clone

func (v *Value) Clone() *Value

Clone returns a deep copy of v.

func (*Value) Equal

func (v *Value) Equal(o *Value) bool

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

func (v *Value) FormatText() string

FormatText is what an allow-list format is matched against: strings without their quotes, every other scalar as its ScalarText.

func (*Value) Keys

func (v *Value) Keys() []string

Keys returns the object's keys in source order, duplicates included.

func (*Value) ScalarText

func (v *Value) ScalarText() string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL