simplejsonext

package module
v0.0.0-...-b44163b Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: Apache-2.0 Imports: 13 Imported by: 1

README

Simple extended JSON parser

This is a simple library open-source code for extended-JSON parsing. It understands (and emits) JSON containing Infinity and NaN numbers as unquoted tokens (an extension shared by Python's json library, among others) and it only decodes simply typed values. It (currently) does not know how to decode JSON into structs and their fields, but rather all JSON {objects} become map[string]any, all JSON [arrays] become []any, and all JSON values are represented within any values. (any in golang is synonymous with and shorthand for interface{}, a dynamically typed value type with no constraints and no guaranteed interface.)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) (b []byte, err error)

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

func MarshalToString(v interface{}) (s string, err error)

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

func Unmarshal(b []byte) (any, error)

Unmarshal decodes a JSON representation from b as a generic value: int64, float64, string, bool, nil, []any, or map[string]any.

func UnmarshalObject

func UnmarshalObject(b []byte) (map[string]any, error)

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

func UnmarshalObjectString(s string) (map[string]any, error)

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

func UnmarshalString(s string) (any, error)

UnmarshalString decodes a JSON representation from b as a generic value: int64, float64, string, bool, nil, []any, or map[string]any.

func WalkDeNaN

func WalkDeNaN(obj interface{}) interface{}

WalkDeNaN recursively traverses a simple JSON value, recursively replacing NaN and Infinity values with a corresponding string value. This modifies objects and arrays in-place.

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

func NewEmitter(w io.Writer) Emitter

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 NewParser

func NewParser(r io.Reader) Parser

NewParser creates a new parser that parses the given reader.

func NewParserFromSlice

func NewParserFromSlice(data []byte) Parser

NewParserFromSlice creates a new parser for the given slice.

func NewParserFromString

func NewParserFromString(data string) Parser

NewParserFromString creates a new parser for the given string.

Jump to

Keyboard shortcuts

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