simplejsonext

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

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

Go to latest
Published: Mar 21, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

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 v to a byte slice returned in b.

func MarshalToString

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

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)

func UnmarshalObjectString

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

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 {
	Emit(val any) error
	Reset(io.Writer)
}

func NewEmitter

func NewEmitter(w io.Writer) Emitter

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 return 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