wirejson

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 12 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 is the JSON reader behind the generated model decoders (models/models_gen.go) and the WebSocket frame router.

A decoder reads the text once, front to back, and validates every byte it passes, including values it skips. It accepts and rejects exactly what encoding/json accepted with the SDK's earlier decoders, and produces the same values:

  • keys match field names exactly, then without case (encoding/json's folding);
  • unknown keys are skipped but must still be valid JSON;
  • a repeated key decodes again into the value already there (objects merge, lists reuse their elements), as encoding/json does;
  • strings are unescaped like encoding/json: invalid UTF-8 and lone surrogates become U+FFFD;
  • nesting deeper than 10000 is an error.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoLite = errors.New("type has no lite encoding")

ErrNoLite is the error for a type with no lite form (edge types, or any type that is not a model).

View Source
var ErrNull = errors.New("required field cannot be null")

ErrNull is the error for null in a required field that is not nullable.

Functions

func At

func At(err error, typeName, field string) error

At records that err happened in field (a JSON name) of the model typeName. A type error gets the field in its path; any other error, except a syntax error, is wrapped once, at the innermost field. Error paths stay out of line so the generated decoders stay small.

func DecodeAny

func DecodeAny(r *Reader) (any, error)

DecodeAny decodes the next value as json.Unmarshal into an `any` does: map[string]any (a repeated key keeps its last value), []any, string, float64, bool or nil.

func DecodeBool

func DecodeBool(r *Reader, dst *bool) error

DecodeBool: true or false; null keeps the old value.

func DecodeBoolPtr

func DecodeBoolPtr(r *Reader, dst **bool) error

DecodeBoolPtr: null sets nil.

func DecodeInt

func DecodeInt[T signed](r *Reader, dst *T) error

DecodeInt is DecodeUint for signed integers.

func DecodeIntPtr

func DecodeIntPtr[T signed](r *Reader, dst **T) error

DecodeIntPtr: null sets nil.

func DecodeQuotedInt

func DecodeQuotedInt[T ~int64](r *Reader, dst *T) error

DecodeQuotedInt is DecodeQuotedUint for int64.

func DecodeQuotedUint

func DecodeQuotedUint[T ~uint64](r *Reader, dst *T) error

DecodeQuotedUint is a required uint64: a string holding a JSON integer (whitespace around it is allowed); "null" in the string keeps the old value.

func DecodeRaw

func DecodeRaw(r *Reader, dst *json.RawMessage) error

DecodeRaw copies any value's text (null is kept as "null").

func DecodeString

func DecodeString[S ~string](r *Reader, dst *S) error

DecodeString: a string; null keeps the old value.

func DecodeStringIntPtr

func DecodeStringIntPtr[T ~int64](r *Reader, dst **T) error

DecodeStringIntPtr is DecodeStringUintPtr for int64.

func DecodeStringList

func DecodeStringList[S ~string](r *Reader, dst *[]S) error

DecodeStringList is a list of strings (null items keep the item already there); null is nil.

func DecodeStringUintPtr

func DecodeStringUintPtr[T ~uint64](r *Reader, dst **T) error

DecodeStringUintPtr is an optional uint64 sent as a string.

func DecodeTime

func DecodeTime(r *Reader, dst *time.Time) error

DecodeTime is a timestamp: a string of integer nanoseconds (strconv.ParseInt, so a leading '+' is allowed), as UTC. A bad text gives strconv.ParseInt's error, as before.

func DecodeTimePtr

func DecodeTimePtr(r *Reader, dst **time.Time) error

DecodeTimePtr: null sets nil.

func DecodeUint

func DecodeUint[T unsigned](r *Reader, dst *T) error

DecodeUint: a JSON number that is an integer in range (strconv.ParseUint of its text); null keeps the old value.

func DecodeUint64s

func DecodeUint64s[S ~[]uint64](r *Reader, dst *S) error

DecodeUint64s is a list of 64-bit integers, each a string or a number holding only digits (the string is not unescaped). null is an empty list. The list is always replaced.

func DecodeUintList

func DecodeUintList[T unsigned](r *Reader, dst *[]T) error

DecodeUintList is DecodeStringList for small unsigned integers.

func DecodeUintPtr

func DecodeUintPtr[T unsigned](r *Reader, dst **T) error

DecodeUintPtr: null sets nil.

func FinishLite

func FinishLite(r *Reader, enc LiteEncoder, full []byte) ([]byte, error)

FinishLite runs a generated lite encoder over the whole text r reads (full is that text).

func Fold

func Fold(out, key []byte) []byte

Fold appends key folded as encoding/json folds field names (ASCII upper case, other runes to the smallest rune of their case-fold set), so two keys match without case when their folds are equal.

func FoldedIndex

func FoldedIndex(key []byte, folded []string) int

FoldedIndex returns the index of the name in folded (names already folded) that key matches without case, or -1. It is the slow path after an exact match failed.

func Grow

func Grow[E any](s []E, i int) []E

Grow makes s[i] addressable for decoding list item i, as encoding/json does: an item already there is decoded into again.

func LiteList

func LiteList(r *Reader, out []byte, item LiteEncoder) ([]byte, error)

LiteList is a lite encoder for a list of items that each have encoder item (null is copied).

func Lookup

func Lookup[T any]() func([]byte, *T) error

Lookup returns the generated decoder of T, or nil.

func NoLiteName

func NoLiteName(typeName string, key []byte) error

NoLiteName is the error for a key the type typeName has no lite name for.

func Register

func Register[T any](unmarshal func([]byte, *T) error)

Register records the generated decoder of a model type.

func RegisterLite

func RegisterLite[T any](unmarshal func([]byte, *T) error, toLite func([]byte) ([]byte, error))

RegisterLite records the generated lite codec of a model type: its lite decoder and its full-to-lite renamer.

func ToLite

func ToLite[T any](full []byte) ([]byte, error)

ToLite rewrites full, the full-encoding text of a T (as encoding/json writes it), into the lite encoding: same values, each key renamed by its type. A key T has no lite name for is an error.

func Trim

func Trim[E any](s []E, n int) []E

Trim ends a list after n items; an empty list is a new empty slice.

func Unexpected

func Unexpected[T any](r *Reader) error

Unexpected is Reader.Unexpected for T.

func Unmarshal

func Unmarshal[T any](data []byte, v *T) error

Unmarshal decodes data into v with T's generated decoder, or with encoding/json when T has none.

func UnmarshalLite

func UnmarshalLite[T any](data []byte, v *T) error

UnmarshalLite decodes lite-encoding data into v with T's generated decoder. There is no encoding/json fallback: a type without one is ErrNoLite.

func ValidNumber

func ValidNumber(s []byte) bool

ValidNumber reports whether s is a JSON number.

Types

type LiteEncoder

type LiteEncoder func(r *Reader, out []byte) ([]byte, error)

LiteEncoder appends the lite form of the full-encoding value at r to out (generated liteFrom<T>).

type Reader

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

Reader reads one JSON text. The zero value is not usable; use NewReader.

func NewLiteReader

func NewLiteReader(data []byte) Reader

NewLiteReader reads data in the lite encoding: the generated decoders match each type's lite keys (exactly, without case folding) instead of its full ones. Everything else is the same.

func NewReader

func NewReader(data []byte) Reader

NewReader reads data in the full encoding.

func (*Reader) AppendRaw

func (r *Reader) AppendRaw(out []byte) ([]byte, error)

AppendRaw validates one value and appends its text to out.

func (*Reader) Bool

func (r *Reader) Bool() (bool, error)

Bool reads true or false.

func (*Reader) Elem

func (r *Reader) Elem(first bool) (ok bool, err error)

Elem moves to the next element of an open array. ok is false at the closing ']', which it consumes.

func (*Reader) End

func (r *Reader) End() error

End checks that only whitespace is left.

func (*Reader) Fail

func (r *Reader) Fail(err error) error

Fail is the error a whole-text decode returns for err: encoding/json checks the whole text before decoding, so invalid JSON anywhere wins over an error found earlier in the text.

func (*Reader) Lite

func (r *Reader) Lite() bool

Lite reports whether keys are lite names.

func (*Reader) Member

func (r *Reader) Member(first bool) (key []byte, ok bool, err error)

Member reads the next key of an open object and the ':' after it. ok is false at the closing '}', which it consumes. first is true for the first call after OpenObject.

func (*Reader) Next

func (r *Reader) Next() byte

Next returns the first byte of the next value (after whitespace), or 0 at the end of the input.

func (*Reader) Null

func (r *Reader) Null() (bool, error)

Null consumes a null and reports true, or reports false and consumes nothing.

func (*Reader) Number

func (r *Reader) Number() ([]byte, error)

Number reads a number and returns its text.

func (*Reader) OpenArray

func (r *Reader) OpenArray() error

OpenArray consumes '['.

func (*Reader) OpenObject

func (r *Reader) OpenObject() error

OpenObject consumes '{'.

func (*Reader) Raw

func (r *Reader) Raw() ([]byte, error)

Raw validates one value and returns its text.

func (*Reader) Skip

func (r *Reader) Skip() error

Skip validates and passes over one value.

func (*Reader) String

func (r *Reader) String() ([]byte, error)

String reads a string and returns its unescaped bytes: a slice of the input when the string has no escapes and is valid UTF-8, otherwise a new slice.

func (*Reader) Unexpected

func (r *Reader) Unexpected(t reflect.Type) error

Unexpected is the error for a value of the wrong kind for t: a *json.SyntaxError when the value is not valid JSON, otherwise a *json.UnmarshalTypeError.

Jump to

Keyboard shortcuts

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