Documentation
¶
Index ¶
- Variables
- func Append(dst []byte, v any, opts ...Option) ([]byte, error)
- func AsMap(v any) (map[string]any, bool)
- func Clone(v any) any
- func Decode[T ~string | ~[]byte](data T) (any, error)
- func DecodePartial[T ~string | ~[]byte](data T) (any, error)
- func DecodePlain[T ~string | ~[]byte](data T) (any, error)
- func DecodePrefix[T ~string | ~[]byte](data T) (any, int, error)
- func DecodeWith[T ~string | ~[]byte](data T, opts ...DecodeOption) (any, error)
- func Encode(v any, opts ...Option) (string, error)
- func Ordered(v any) any
- func Plain(v any) any
- func Text(data []byte) (string, error)
- func Validate[T ~string | ~[]byte](data T) error
- type DecodeOption
- type Object
- func (o *Object) All() iter.Seq2[string, any]
- func (o *Object) Clone() *Object
- func (o *Object) Delete(key string) bool
- func (o *Object) Get(key string) (any, bool)
- func (o *Object) Has(key string) bool
- func (o *Object) Keys() []string
- func (o *Object) Len() int
- func (o *Object) Map() map[string]any
- func (o *Object) MarshalJSON() ([]byte, error)
- func (o *Object) Merge(other *Object)
- func (o *Object) Rename(from, to string) bool
- func (o *Object) Set(key string, v any)
- func (o *Object) String() string
- func (o *Object) Take(key string) (any, bool)
- func (o *Object) UnmarshalJSON(data []byte) error
- func (o *Object) Value(key string) any
- func (o *Object) ValueOr(key string, def any) any
- type Option
- type SyntaxError
Constants ¶
This section is empty.
Variables ¶
var ErrNotObject = errors.New("json: value is not an object")
Reported by DecodeObject and UnmarshalJSON when the input holds a JSON value other than an object
Functions ¶
func AsMap ¶
The plain map v holds: a map[string]any itself, an *Object's keys and values in a new map; false for anything else
func Clone ¶
Deep copy of a decoded value: every *Object, map[string]any and []any copied at any depth, other values as they are
func Decode ¶
Value of the JSON text in data: objects as *Object in document order, arrays as []any, integers that fit as int64, other numbers as float64, NaN and Infinity accepted, strings made valid UTF-8, a byte order mark and UTF-16 or UTF-32 input understood, anything after the value refused
func DecodePartial ¶
Value of UTF-8 JSON text cut off anywhere, as streamed tool arguments arrive: open arrays and objects closed, a member or element the text ends inside (an unfinished string, key or literal, a number still missing digits after its sign, point or exponent) left out, a number cut between digits kept as read; shapes as Decode gives them
func DecodePlain ¶
Value of the JSON text in data in the shapes encoding/json gives an any, objects as map[string]any and every number as float64, otherwise read as Decode reads it
func DecodePrefix ¶
First JSON value in the UTF-8 text data after any leading space, in the shapes Decode gives, and the byte offset in data just past it; what follows is not looked at
func DecodeWith ¶
func DecodeWith[T ~string | ~[]byte](data T, opts ...DecodeOption) (any, error)
Value of the JSON text in data as Decode reads it under the given options
func Encode ¶
JSON text of v: compact, UTF-8, no HTML escaping, *Object keys in insertion order, map keys sorted, nil slices and maps as [] and {}, every float with a fraction or exponent (1.0, never 1) so it decodes back as a float, NaN and infinities as null, structs by their json tags, json.Marshaler and encoding.TextMarshaler honored, errors as their text
func Ordered ¶
Copy of a value with every string-keyed map at any depth turned into an *Object with its keys sorted and every slice or array whose elements are not bytes into []any, other values as they are
func Plain ¶
Copy of a decoded value with every *Object turned into a map[string]any at any depth, maps and slices copied, other values as they are
func Text ¶
UTF-8 text of JSON bytes written in UTF-8, UTF-16 or UTF-32, a byte order mark dropped; on bad input the text with U+FFFD in place of each bad sequence and a *SyntaxError naming the first
Types ¶
type DecodeOption ¶
type DecodeOption func(*decoder)
Setting for DecodeWith
func BigInts ¶
func BigInts() DecodeOption
Integers past the int64 range decoded exactly as *big.Int instead of rounded to float64
func ControlChars ¶
func ControlChars() DecodeOption
Raw control characters inside strings taken as they are instead of refused
func UniqueKeys ¶
func UniqueKeys() DecodeOption
A key repeated inside one object refused instead of its last value kept at its first position
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
JSON object that keeps its keys in insertion order, a key set again keeping its first position
func AsObject ¶
The object v holds: an *Object itself, a map[string]any copied into a new object with its keys sorted; false for anything else
func DecodeObject ¶
Object of the JSON text in data as Decode reads it, ErrNotObject wrapped when the text holds another kind of value
func ObjectOf ¶
Object of alternating string keys and values in the order given; panics on an odd count or a key that is not a string
func (*Object) All ¶
Keys and values in insertion order over the keys present when the iteration started: one deleted on the way is still visited, one added is not
func (*Object) Delete ¶
Removes key, reporting whether it was present; slices Keys returned earlier keep their contents, and an iteration of All in progress goes on over the keys it started with
func (*Object) Keys ¶
Keys in insertion order; the slice belongs to the object and must not be modified
func (*Object) MarshalJSON ¶
Compact JSON of the object with its keys in insertion order
func (*Object) Merge ¶
Sets every key of other in other's order, keys already present keeping their position
func (*Object) Rename ¶
Moves the value under from to the key to at the same position; false when from is absent or to is already present
func (*Object) Set ¶
Stores v under key, a new key going last and an existing one keeping its position
func (*Object) String ¶
Compact JSON text of the object, values with no JSON form written as their fmt text and a failure as %!(error)
func (*Object) UnmarshalJSON ¶
Replaces the contents with the JSON object in data, leaving them as they are for JSON null
type Option ¶
type Option func(*encoder)
Setting for Encode and Append
func ASCII ¶
func ASCII() Option
Every character outside printable ASCII that is not escaped anyway (DEL and everything past it) written as a \u escape, astral ones as a surrogate pair
func Convert ¶
Hook for values with no JSON form (functions, channels, complex numbers, maps with unusable keys): fn's result is encoded in their place
func Indent ¶
Each array element and object member on its own line, indented n spaces a level and a space after each colon; zero puts them on their own lines without indentation
type SyntaxError ¶
type SyntaxError struct {
// What is wrong, without the position
Msg string
// Byte offset of the problem in the UTF-8 text read: past a leading byte order mark, and in the transcoded text for UTF-16 or UTF-32 input
Offset int
// Line of the problem, counted from 1
Line int
// Column of the problem in characters, counted from 1
Column int
}
Malformed JSON input with the place it was found
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
The message with its line and column