wirejson

package
v1.1.68-test Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wirejson serializes an API object — the map[string]any shape every handler builds — to JSON bytes.

It exists because that one operation is the single largest CPU line item in the service. Profiling an org-wide workload `-query` under load put encoding/json.mapEncoder at 34% of all CPU: for every response the reflect-based encoder walks an interface tree, and for every nested object it allocates a slice of reflect.Values to sort. The values in these maps come from a JSON decode, so the type set is closed and tiny — a type switch does the same work without reflection.

It is NOT a general JSON encoder and must never become one. The contract is that Append produces the bytes httpx.WriteJSON's encoder already produced — an encoding/json.Encoder with SetEscapeHTML(FALSE), which is what matches JSON.stringify and therefore the TS service: `<`, `>` and `&` go out as themselves. Anything this package does not recognize is handed back to encoding/json rather than guessed at. The contract is enforced differentially (encode_test.go): the two encoders are compared byte-for-byte, including over random trees of adversarial strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(dst []byte, v any) ([]byte, error)

Append appends the JSON encoding of v to dst.

func AppendBool

func AppendBool(dst []byte, b bool) []byte

func AppendFloat

func AppendFloat(dst []byte, f float64) ([]byte, error)

func AppendInt

func AppendInt(dst []byte, i int64) []byte

func AppendStdlib

func AppendStdlib(dst []byte, v any) ([]byte, error)

AppendStdlib encodes v with encoding/json (SetEscapeHTML(false)) — the fallback for a field type the row encoder does not model.

func AppendString

func AppendString(dst []byte, s string) []byte

func AppendUint

func AppendUint(dst []byte, u uint64) []byte

func Decode

func Decode(data []byte, out *any) error

Decode parses JSON into the generic Go shapes encoding/json produces for an `any` target: map[string]any, []any, string, float64, bool, nil.

It is the read-side twin of Append, and it exists for the same reason. A jsonb column arrives as bytes and is parsed into a map so handlers can work with it; profiling a paged query put encoding/json.Unmarshal inside store.JSONB.Scan at 24% of all CPU. The reflect-driven decoder is doing generic work that a parser targeting exactly these six shapes does not have to do.

Same contract as the encoder, and the same enforcement: the result must be DeepEqual to what json.Unmarshal would have produced — including float64 for every number, last-wins duplicate keys, and U+FFFD for invalid UTF-8 — with anything malformed reported as an error rather than guessed at. decode_test.go compares the two over random documents.

func Lookup

func Lookup(data []byte, path []string) (any, bool)

Lookup returns the value at a dot-path inside a JSON document, decoded exactly as json.Unmarshal into `any` would produce it. found is false when a segment is missing or its parent is not an object — the same answer walking a map[string]any gives.

An empty path returns the whole document. Duplicate keys resolve last-wins, as encoding/json does.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal returns the JSON encoding of v.

func SetStructAppender

func SetStructAppender(fn func(dst []byte, v any) ([]byte, bool, error))

SetStructAppender installs the appender for row structs. The store registers its plan-based encoder here at init; wirejson cannot import the store.

func Transcode

func Transcode(dst, src []byte) ([]byte, error)

Transcode rewrites a jsonb column's bytes as the bytes this service puts on the wire, in ONE pass and without building a single map.

A jsonb column arrives from Postgres as JSON text. The read path used to parse it into map[string]any, copy that into the API object, and encode it again — measured at 354µs per 50-row page for the decode alone, and the largest single item in the service. None of that is needed to answer a list or query: nothing reads `spec`.

It is a transcode rather than a straight copy because Postgres's text is close to, but not the same as, what encoding/json would have produced:

  • Numbers. jsonb keeps `numeric`, so 1e21 comes back as 1000000000000000000000 and 1e-7 as 0.0000001. Go renders the float64 as 1e+21 and 1e-07. Numbers are therefore re-rendered through float64 — the same lossy step the old decode took, so the bytes do not move and neither does the precision.
  • Whitespace. Postgres emits `{"a": 1, "b": 2}`; the encoder emits it tight.
  • U+2028/U+2029. Postgres passes them through raw; the encoder escapes them, and so must this, or a spec string could break a JavaScript consumer.

KEY ORDER is deliberately left as Postgres gives it (length, then bytewise). That is a change: the map path sorted keys alphabetically. It is a change TOWARD the TS service, which hands the driver's parsed object straight to JSON.stringify and therefore emits Postgres's order too — see INTENTIONAL-DIVERGENCES C12.

Invalid input is reported rather than guessed at; callers fall back to the decode.

Types

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor is a streaming reader over one JSON document.

func NewCursor

func NewCursor(data []byte) *Cursor

NewCursor positions a cursor on the document's top-level value.

func (*Cursor) Array

func (c *Cursor) Array(fn func(i int) error) error

Array consumes an array, calling fn for each element with the cursor on it.

func (*Cursor) Bool

func (c *Cursor) Bool() (bool, error)

func (*Cursor) Done

func (c *Cursor) Done() error

Done reports an error if anything but whitespace follows the consumed document.

func (*Cursor) Generic

func (c *Cursor) Generic() (any, error)

Generic consumes the value as a tree, exactly as Decode would produce it.

func (*Cursor) Null

func (c *Cursor) Null() error

func (*Cursor) Number

func (c *Cursor) Number() (float64, error)

func (*Cursor) Object

func (c *Cursor) Object(fn func(key string) error) error

Object consumes an object, calling fn for each key with the cursor on the value. A duplicate key is delivered again (last wins is the callback's business).

func (*Cursor) Peek

func (c *Cursor) Peek() (Token, error)

Peek reports the kind of the value under the cursor without consuming it.

func (*Cursor) String

func (c *Cursor) String() (string, error)

type Token

type Token uint8

Token is the kind of the value under the cursor.

const (
	TokObject Token = iota + 1
	TokArray
	TokString
	TokNumber
	TokBool
	TokNull
	// TokOther is never produced from bytes; a tree source uses it for in-process
	// values that have no JSON token (a typed slice, an int).
	TokOther
)

Jump to

Keyboard shortcuts

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