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 ¶
- func Append(dst []byte, v any) ([]byte, error)
- func AppendBool(dst []byte, b bool) []byte
- func AppendFloat(dst []byte, f float64) ([]byte, error)
- func AppendInt(dst []byte, i int64) []byte
- func AppendStdlib(dst []byte, v any) ([]byte, error)
- func AppendString(dst []byte, s string) []byte
- func AppendUint(dst []byte, u uint64) []byte
- func Decode(data []byte, out *any) error
- func Lookup(data []byte, path []string) (any, bool)
- func Marshal(v any) ([]byte, error)
- func SetStructAppender(fn func(dst []byte, v any) ([]byte, bool, error))
- func Transcode(dst, src []byte) ([]byte, error)
- type Cursor
- func (c *Cursor) Array(fn func(i int) error) error
- func (c *Cursor) Bool() (bool, error)
- func (c *Cursor) Done() error
- func (c *Cursor) Generic() (any, error)
- func (c *Cursor) Null() error
- func (c *Cursor) Number() (float64, error)
- func (c *Cursor) Object(fn func(key string) error) error
- func (c *Cursor) Peek() (Token, error)
- func (c *Cursor) String() (string, error)
- type Token
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendBool ¶
func AppendStdlib ¶
AppendStdlib encodes v with encoding/json (SetEscapeHTML(false)) — the fallback for a field type the row encoder does not model.
func AppendString ¶
func AppendUint ¶
func Decode ¶
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 ¶
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 SetStructAppender ¶
SetStructAppender installs the appender for row structs. The store registers its plan-based encoder here at init; wirejson cannot import the store.
func Transcode ¶
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 (*Cursor) Done ¶
Done reports an error if anything but whitespace follows the consumed document.
func (*Cursor) Object ¶
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).