Documentation
¶
Index ¶
- func AppendCompact(dst, raw []byte) ([]byte, error)
- func AppendFloat(dst []byte, value float64) []byte
- func AppendString(dst []byte, value string) []byte
- func Decode(data []byte) (any, bool)
- func DecodeWTF8Surrogate(value string) (uint16, bool)
- func EachMember(data []byte, visit func(name, value []byte) bool)
- func Elements(data []byte) []json.RawMessage
- func Marshal(value any) ([]byte, error)
- func MarshalIndent(value any, prefix, indent string) ([]byte, error)
- func MarshalString(value string) ([]byte, error)
- func Members(data []byte) (map[string]json.RawMessage, bool)
- func MessageRoleAndText(raw json.RawMessage) (string, string)
- func Stringified(data []byte) bool
- func UnmarshalString(data []byte) (string, error)
- func UnmarshalStringToken(data []byte) (string, error)
- func Valid(data []byte) bool
- type OrderedMember
- type OrderedObject
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendCompact ¶ added in v0.16.0
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
AppendFloat appends the finite number value as JSON.stringify writes it.
func AppendString ¶ added in v0.16.0
AppendString appends value as MarshalString encodes it.
func Decode ¶ added in v0.16.0
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 ¶
DecodeWTF8Surrogate returns the leading UTF-16 surrogate encoded as WTF-8.
func EachMember ¶ added in v0.16.0
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 ¶
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 ¶
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 ¶
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
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 ¶
UnmarshalString decodes a JSON string while retaining lone UTF-16 surrogates as WTF-8 so MarshalString can reproduce JSON.stringify output.
func UnmarshalStringToken ¶
UnmarshalStringToken decodes the first JSON string in a segment consumed by json.Decoder.Token. The segment may include separators and whitespace.
Types ¶
type OrderedMember ¶
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.