Documentation
¶
Index ¶
- func Marshal(v interface{}) (b []byte, err error)
- func MarshalToString(v interface{}) (s string, err error)
- func Unmarshal(b []byte) (any, error)
- func UnmarshalObject(b []byte) (map[string]any, error)
- func UnmarshalObjectString(s string) (map[string]any, error)
- func UnmarshalString(s string) (any, error)
- func WalkDeNaN(obj interface{}) interface{}
- type Emitter
- type Parser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal writes the JSON representation of the given value to a byte slice.
The given value is expected to contain only supported types, which include: nil, bool, integers, floats, string, []byte (as a base64 encoded string), time.Time (written as an RFC3339 string), error (written as a string), and pointers/slices/string-keyed maps of supported types. If a type in v is not supported, an error will be returned.
func MarshalToString ¶
MarshalToString writes the JSON representation of the given value to a string.
The given value is expected to contain only supported types, which include: nil, bool, integers, floats, string, []byte (as a base64 encoded string), time.Time (written as an RFC3339 string), error (written as a string), and pointers/slices/string-keyed maps of supported types. If a type in v is not supported, an error will be returned.
func Unmarshal ¶
Unmarshal decodes a JSON representation from b as a generic value: int64, float64, string, bool, nil, []any, or map[string]any.
func UnmarshalObject ¶
UnmarshalObject decodes JSON as a simply-typed JSON object and returns it. If the JSON is a value of a type other than object, such as JSON null or a JSON string, an error will be returned.
func UnmarshalObjectString ¶
UnmarshalObjectString decodes JSON as a simply-typed JSON object and returns it. If the JSON is a value of a type other than object, such as JSON null or a JSON string, an error will be returned.
func UnmarshalString ¶
UnmarshalString decodes a JSON representation from b as a generic value: int64, float64, string, bool, nil, []any, or map[string]any.
Types ¶
type Emitter ¶
type Emitter interface {
// Writes the given value to the wrapped Writer. If the value or part of the
// value is not a supported type, an error will be returned with JSON only
// partially written.
//
// Supported types include: nil, bool, integers, floats, string, []byte
// (as a base64 encoded string), time.Time (written as an RFC3339 string),
// error (written as a string), and pointers/slices/string-keyed maps of
// supported types.
Emit(val any) error
// Replaces the Writer that this Emitter writes to.
Reset(io.Writer)
}
Reusable object that can encode simple JSON values to any Writer.
func NewEmitter ¶
Creates a new Emitter wrapping the given Writer.
type Parser ¶
type Parser interface {
// Parse JSON from the front of the contained data as a simply-typed value
// and return it. If the data is empty, the exact error io.EOF will be
// returned.
Parse() (any, error)
// ParseObject parses JSON from the front of the contained data as a
// simply-typed JSON object and returns it. If the JSON is a value of a type
// other than object, an error will be returned. If the data is empty, the
// exact error io.EOF will be returned.
ParseObject() (map[string]any, error)
// NextLine consumes whitespace up to the next newline, returning an error
// if something other than whitespace exists before the next newline, or
// returning the exact error io.EOF if the end of data is found first. This
// method reads until exactly the next '\n' newline character, not any other
// combination of '\r' and '\n'; it will work with "\r\n" and "\n" newlines
// only.
NextLine() error
// IterLines returns a Range-func iterable for reading JSONL.
//
// Returns an iterable go1.22+ RangeFunc that yields each line of a JSONL
// until the end of data. If an error occurs, it will be yielded on its own
// and iteration will stop.
//
// See: https://go.dev/wiki/RangefuncExperiment
IterLines() func(func(any, error) bool)
// IterObjectLines returns a Range-func iterable for reading JSONL,
// enforcing that each line must also be a JSON object rather than another
// kind of JSON value.
//
// Returns an iterable go1.22+ RangeFunc that yields each line of a JSONL
// until the end of data. If an error occurs, it will be yielded on its own
// and iteration will stop.
//
// See: https://go.dev/wiki/RangefuncExperiment
IterObjectLines() func(func(map[string]any, error) bool)
// CheckEmpty checks that the remaining data is all whitespace, returning an
// error if not.
CheckEmpty() error
// UnmarshalFull parses JSON from the front of the contained data, then
// checks if there is any data left after. If there is, the value will still
// be returned but there will be a "not empty" error. If the data is empty,
// the exact error io.EOF will be returned.
UnmarshalFull() (any, error)
// Reset the parser with a new io.Reader.
Reset(io.Reader)
// ResetSlice resets the parser with a new byte slice.
ResetSlice([]byte)
// ResetString resets the parser with a new string.
ResetString(string)
}
func NewParserFromSlice ¶
NewParserFromSlice creates a new parser for the given slice.
func NewParserFromString ¶
NewParserFromString creates a new parser for the given string.