Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonwire

package
v0.17.6 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendCompact added in v0.16.0

func AppendCompact(dst, raw []byte) ([]byte, error)

AppendCompact appends the JSON value raw as Marshal encodes a json.RawMessage holding it: compacted, with Marshal's -0 and line separator fixes. An empty raw value encodes as null.

func AppendFloat added in v0.16.0

func AppendFloat(dst []byte, value float64) []byte

AppendFloat appends the finite number value as JSON.stringify writes it.

func AppendString added in v0.16.0

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

AppendString appends value as MarshalString encodes it.

func Decode added in v0.16.0

func Decode(data []byte) (any, bool)

Decode decodes data as json.Unmarshal into an any does: objects as map[string]any, arrays as []any, numbers as float64. It reports false, leaving data to encoding/json, for anything it might decode differently: invalid JSON, duplicate names, strings that are not valid UTF-8 or escape a surrogate, and numbers out of float64's range.

func DecodeWTF8Surrogate

func DecodeWTF8Surrogate(value string) (uint16, bool)

DecodeWTF8Surrogate returns the leading UTF-16 surrogate encoded as WTF-8.

func EachMember added in v0.16.0

func EachMember(data []byte, visit func(name, value []byte) bool)

EachMember calls visit with the name, unescaped, and the raw value of each member of the JSON object data until visit returns false. data must be valid JSON; a value that is not an object has no members. Scanning validated JSON this way skips the allocations of decoding it again.

func Elements added in v0.16.0

func Elements(data []byte) []json.RawMessage

Elements returns the raw elements of the JSON array data, which must be valid JSON; a value that is not an array has none.

func Marshal

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

Marshal follows JSON.stringify's string escaping rather than encoding/json's HTML-safe defaults. JavaScript leaves <, >, &, U+2028, and U+2029 literal.

func MarshalIndent

func MarshalIndent(value any, prefix, indent string) ([]byte, error)

MarshalIndent matches JSON.stringify(value, null, 2)-style layout when called with ("", " "): Marshal's escaping plus json.Indent, which inserts whitespace without re-escaping.

func MarshalString

func MarshalString(value string) ([]byte, error)

MarshalString preserves WTF-8 encoded UTF-16 surrogates so Go can carry JavaScript strings produced by code-unit slicing through a JSON wire format.

func Members added in v0.16.0

func Members(data []byte) (map[string]json.RawMessage, bool)

Members decodes the JSON object data into its raw members as json.Unmarshal into a map[string]json.RawMessage does, sharing data's bytes. It reports false, leaving data to encoding/json, unless data is a valid object whose names are distinct and valid UTF-8.

func MessageRoleAndText

func MessageRoleAndText(raw json.RawMessage) (string, string)

MessageRoleAndText preserves upstream's concatenation of text blocks with no separator.

func Stringified added in v0.16.0

func Stringified(data []byte) bool

Stringified reports whether data is one JSON value spelled as JSON.stringify writes the value JSON.parse reads from it, so compacting or normalizing it changes nothing. It accepts a subset: no whitespace, strings escaping only quotes, backslashes and the named controls, integers of up to fifteen digits, and objects whose few names are distinct and do not start with a digit (JavaScript orders array indexes first).

func UnmarshalString

func UnmarshalString(data []byte) (string, error)

UnmarshalString decodes a JSON string while retaining lone UTF-16 surrogates as WTF-8 so MarshalString can reproduce JSON.stringify output.

func UnmarshalStringToken

func UnmarshalStringToken(data []byte) (string, error)

UnmarshalStringToken decodes the first JSON string in a segment consumed by json.Decoder.Token. The segment may include separators and whitespace.

func Valid added in v0.16.0

func Valid(data []byte) bool

Valid reports whether data is one JSON value, as json.Valid does. It consults encoding/json only for input it does not accept itself: invalid, nested deeper than encoding/json allows, or escaping a UTF-16 surrogate.

Types

type OrderedMember

type OrderedMember struct {
	Name  string
	Value any
}

OrderedMember is one name/value pair of an OrderedObject.

type OrderedObject

type OrderedObject []OrderedMember

OrderedObject is a JSON object that marshals its members in insertion order, matching JavaScript's object key ordering on the wire.

func (*OrderedObject) Delete

func (object *OrderedObject) Delete(name string)

Delete removes the first member with the given name, if present.

func (OrderedObject) MarshalJSON

func (object OrderedObject) MarshalJSON() ([]byte, error)

func (*OrderedObject) Set

func (object *OrderedObject) Set(name string, value any)

Set replaces the value of the first member with the given name in place, or appends a new member.

func (OrderedObject) Value

func (object OrderedObject) Value(name string) (any, bool)

Value returns the value of the first member with the given name.

Jump to

Keyboard shortcuts

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