Documentation
¶
Overview ¶
Package json5 implements encoding and decoding of JSON5 as defined by https://spec.json5.org/. The API mirrors the standard library encoding/json package so that migrating existing code is straightforward.
JSON5 is a superset of JSON that adds support for comments, trailing commas, unquoted object keys, single-quoted strings, hexadecimal numbers, Infinity, NaN, and additional whitespace characters.
Struct tags use the key "json5" (falling back to "json" if "json5" is absent) and follow the same conventions as encoding/json.
Index ¶
- func Marshal(v any) ([]byte, error)
- func MarshalIndent(v any, prefix, indent string) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func Valid(data []byte) bool
- type Decoder
- type Encoder
- type Entry
- type InvalidUnmarshalError
- type Marshaler
- type MarshalerError
- type Number
- type OrderedMap
- func (m *OrderedMap) Delete(key string)
- func (m *OrderedMap) Entries() []Entry
- func (m *OrderedMap) Get(key string) (any, bool)
- func (m *OrderedMap) Keys() []string
- func (m *OrderedMap) Len() int
- func (m *OrderedMap) MarshalJSON5() ([]byte, error)
- func (m *OrderedMap) Set(key string, value any)
- func (m *OrderedMap) SetWithComment(key string, value any, comment, inlineComment string)
- func (m *OrderedMap) UnmarshalJSON5(data []byte) error
- type RawMessage
- type SyntaxError
- type UnmarshalTypeError
- type Unmarshaler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns the JSON5 encoding of v.
Marshal traverses the value v recursively using the same rules as encoding/json.Marshal, but outputs valid JSON (which is also valid JSON5).
func MarshalIndent ¶
MarshalIndent is like Marshal but applies Indent to format the output.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes JSON5 values from an input stream. It reads input incrementally using a sliding window buffer, so it can decode a stream of values without loading the entire input into memory.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
func (*Decoder) Decode ¶
Decode reads the next JSON5 value from its input and stores it in the value pointed to by v.
func (*Decoder) DisallowUnknownFields ¶
func (dec *Decoder) DisallowUnknownFields()
DisallowUnknownFields causes the Decoder to return an error when the destination is a struct and the input contains object keys which do not match any non-ignored, exported fields in the destination.
func (*Decoder) UseNumber ¶
func (dec *Decoder) UseNumber()
UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64.
func (*Decoder) UseOrderedMap ¶
func (dec *Decoder) UseOrderedMap()
UseOrderedMap causes the Decoder to unmarshal objects into *OrderedMap instead of map[string]any when the target is an interface{}. This preserves the insertion order of keys.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes JSON5 values to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
type Entry ¶
type Entry struct {
Key string
Value any
Comment string // comment text above the key (without // or /* */ markers)
InlineComment string // comment text on the same line after the value
}
Entry is a key-value pair in an OrderedMap.
type InvalidUnmarshalError ¶
InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves into valid JSON5.
type MarshalerError ¶
type MarshalerError struct {
Type reflect.Type
Err error
// contains filtered or unexported fields
}
MarshalerError describes an error from calling MarshalJSON5 or MarshalJSON.
func (*MarshalerError) Error ¶
func (e *MarshalerError) Error() string
func (*MarshalerError) Unwrap ¶
func (e *MarshalerError) Unwrap() error
type Number ¶
type Number string
Number represents a JSON5 number literal.
func (Number) Float64 ¶
Float64 returns the number as a float64. It handles JSON5 number formats including hexadecimal, Infinity, and NaN.
type OrderedMap ¶
type OrderedMap struct {
// contains filtered or unexported fields
}
OrderedMap is a JSON5 object that preserves the insertion order of keys. It supports O(1) key lookup and O(1) amortized append.
func (*OrderedMap) Delete ¶
func (m *OrderedMap) Delete(key string)
Delete removes a key. It preserves the order of remaining keys.
func (*OrderedMap) Entries ¶
func (m *OrderedMap) Entries() []Entry
Entries returns all entries in insertion order.
func (*OrderedMap) Get ¶
func (m *OrderedMap) Get(key string) (any, bool)
Get returns the value for key and whether it was found.
func (*OrderedMap) Keys ¶
func (m *OrderedMap) Keys() []string
Keys returns the keys in insertion order.
func (*OrderedMap) MarshalJSON5 ¶
func (m *OrderedMap) MarshalJSON5() ([]byte, error)
MarshalJSON5 implements the Marshaler interface, emitting keys in insertion order with comments preserved.
func (*OrderedMap) Set ¶
func (m *OrderedMap) Set(key string, value any)
Set adds or updates a key-value pair. If the key already exists, its value is updated in place (preserving order). If the key is new, it is appended.
func (*OrderedMap) SetWithComment ¶ added in v0.2.0
func (m *OrderedMap) SetWithComment(key string, value any, comment, inlineComment string)
SetWithComment adds or updates a key-value pair with associated comments.
func (*OrderedMap) UnmarshalJSON5 ¶
func (m *OrderedMap) UnmarshalJSON5(data []byte) error
UnmarshalJSON5 implements the Unmarshaler interface.
type RawMessage ¶
type RawMessage []byte
RawMessage is a raw encoded JSON5 value. It implements Marshaler and Unmarshaler and can be used to delay JSON5 decoding or precompute a JSON5 encoding.
func (RawMessage) MarshalJSON5 ¶
func (m RawMessage) MarshalJSON5() ([]byte, error)
MarshalJSON5 returns m as the JSON5 encoding of m.
func (*RawMessage) UnmarshalJSON5 ¶
func (m *RawMessage) UnmarshalJSON5(data []byte) error
UnmarshalJSON5 sets *m to a copy of data.
type SyntaxError ¶
type SyntaxError struct {
Offset int64
// contains filtered or unexported fields
}
SyntaxError is a description of a JSON5 syntax error.
func (*SyntaxError) Error ¶
func (e *SyntaxError) Error() string
type UnmarshalTypeError ¶
type UnmarshalTypeError struct {
Value string
Type reflect.Type
Offset int64
Struct string
Field string
}
UnmarshalTypeError describes a JSON5 value that was not appropriate for a value of a specific Go type.
func (*UnmarshalTypeError) Error ¶
func (e *UnmarshalTypeError) Error() string
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a JSON5 description of themselves.