libjson

package
v1.61.2 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: BSD-3-Clause Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const DefaultPackageName = "json"

DefaultPackageName is the package name used by LoadPackage.

Variables

This section is empty.

Functions

func Builtins

func Builtins(s *Serializer) []*libutil.Builtin

Builtins takes the default serializer for a lisp environment and returns a set of package builtin functions that use it.

func Dump

func Dump(v *lisp.LVal, stringNums bool) ([]byte, error)

Dump serializes the structure of v as a JSON formatted byte slice.

func Load

func Load(b []byte, stringNums bool) *lisp.LVal

Load parses b as JSON and returns an equivalent LVal.

func LoadPackage

func LoadPackage(env *lisp.LEnv) *lisp.LVal

LoadPackage adds the json package to env

func LoadWith added in v1.50.0

func LoadWith(b []byte, opts LoadOpts) *lisp.LVal

LoadWith parses b as JSON under opts and returns an equivalent LVal.

Types

type LoadOpts added in v1.50.0

type LoadOpts struct {
	// MaxAlloc bounds the number of elements in any single array or object in
	// the document.  Zero means unbounded.
	MaxAlloc int

	// StringNumbers decodes every JSON number as a lisp string holding the
	// number's literal text.  It takes precedence over ExactIntegers: a
	// caller that sets both gets strings, exactly as it does today.
	StringNumbers bool

	// ExactIntegers decodes a JSON integer literal as a lisp int rather than
	// a lisp float.
	//
	// encoding/json decodes every JSON number into a float64, and a float64
	// carries 53 bits of integer precision.  So with ExactIntegers false --
	// the default, and the only behaviour that existed before this option --
	// an integer larger than 2^53 is rounded to the nearest float64 on the
	// way in, and NOTHING reports it: the rounded value still compares = to
	// the integer it was meant to be, so a program can read a corrupted
	// identifier, check it against the value it expected, match, and carry
	// on.  That is issue #350.
	//
	// With ExactIntegers true a JSON number whose literal text is written as
	// an integer -- no '.', no exponent -- decodes to a lisp int holding its
	// exact value, and one that does not fit in a lisp int is an ERROR
	// (condition json:integer-range-error) rather than a rounded float.
	// Numbers written with a fraction or an exponent are untouched and still
	// decode as floats.
	//
	// The rule is SYNTACTIC on purpose.  "1e2" denotes an integer but is not
	// written as one, and it keeps decoding to a float; so does "-0", which
	// parses to the integer 0 and would therefore re-encode as "0" rather
	// than the "-0" it produces today.  A rule that depends only on the bytes
	// of the document, and never on the value they denote, is reproducible on
	// every node that reads the same bytes -- which is the property that
	// matters where this package decodes replicated state.
	ExactIntegers bool
}

LoadOpts controls how a JSON document is decoded into lisp values.

The zero value reproduces Load(b, false) exactly -- the behaviour every caller of this package has had since 2018. Every field is an opt-in.

type Serializer

type Serializer struct {
	True             *lisp.LVal
	False            *lisp.LVal
	Null             *lisp.LVal
	UseStringNumbers bool
	// UseExactIntegers is the default for LoadOpts.ExactIntegers used by the
	// package builtins when the caller passes no :exact-integers keyword.  It
	// does NOT affect Load or LoadMax, which take their options as arguments.
	UseExactIntegers bool
}

Serializer defines JSON serialization rules for lisp values.

func DefaultSerializer

func DefaultSerializer() *Serializer

func (*Serializer) Dump

func (s *Serializer) Dump(v *lisp.LVal, stringNums bool) ([]byte, error)

Dump serializes v as JSON and returns any error.

func (*Serializer) DumpBytesBuiltin

func (s *Serializer) DumpBytesBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) DumpMessageBuiltin

func (s *Serializer) DumpMessageBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) DumpStringBuiltin

func (s *Serializer) DumpStringBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) GoError deprecated

func (s *Serializer) GoError(v *lisp.LVal) error

GoError returns an error that represents v. If v is not LError then nil is returned.

Deprecated: GoError is no longer used internally for serialization and should be avoided.

func (*Serializer) GoFloat64 deprecated

func (s *Serializer) GoFloat64(v *lisp.LVal) (float64, bool)

GoFloat64 converts the numeric value that v represents to a float64 and returns it with the value true. If v does not represent a number GoFloat64 returns a false second argument

Deprecated: GoFloat64 is no longer used internally for serialization and should be avoided.

func (*Serializer) GoInt deprecated

func (s *Serializer) GoInt(v *lisp.LVal) (int, bool)

GoInt converts the numeric value that v represents to and int and returns it with the value true. If v does not represent a number GoInt returns a false second argument

Deprecated: GoInt is no longer used internally for serialization and should be avoided.

func (*Serializer) GoMap deprecated

func (s *Serializer) GoMap(v *lisp.LVal, stringNums bool) (map[string]any, bool)

GoMap converts an LSortMap to its Go equivalent and returns it with a true second argument. If v does not represent a map json serializable map GoMap returns a false second argument

Deprecated: GoMap is no longer used internally for serialization and should be avoided.

func (*Serializer) GoSlice deprecated

func (s *Serializer) GoSlice(v *lisp.LVal, stringNums bool) ([]interface{}, bool)

GoSlice returns the string that v represents and the value true. If v does not represent a string GoSlice returns a false second argument

Deprecated: GoSlice is no longer used internally for serialization and should be avoided.

func (*Serializer) GoString deprecated

func (s *Serializer) GoString(v *lisp.LVal) (string, bool)

GoString returns the string that v represents and the value true. If v does not represent a string GoString returns a false second argument

Deprecated: GoString is no longer used internally for serialization and should be avoided.

func (*Serializer) GoValue deprecated

func (s *Serializer) GoValue(v *lisp.LVal, stringNums bool) interface{}

GoValue converts v to its natural representation in Go. Quotes are ignored and all lists are turned into slices. Symbols are converted to strings. The value Nil() is converted to nil. Functions are returned as is.

Deprecated: GoValue is no longer used internally for serialization and should be avoided. It also walks its argument without a bound, so a value that contains itself takes the process down with it (issue #390); Dump refuses such a value with an error instead.

func (*Serializer) Load

func (s *Serializer) Load(b []byte, stringNums bool) *lisp.LVal

Load parses b and returns an LVal representing its structure.

func (*Serializer) LoadBytesBuiltin

func (s *Serializer) LoadBytesBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) LoadMax added in v1.20.0

func (s *Serializer) LoadMax(b []byte, stringNums bool, maxAlloc int) *lisp.LVal

LoadMax is like Load but enforces a maximum allocation size for arrays and maps parsed from JSON. When maxAlloc is 0, no limit is enforced.

func (*Serializer) LoadMessageBuiltin

func (s *Serializer) LoadMessageBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) LoadStringBuiltin

func (s *Serializer) LoadStringBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) LoadWith added in v1.50.0

func (s *Serializer) LoadWith(b []byte, opts LoadOpts) *lisp.LVal

LoadWith parses b under opts and returns an LVal representing its structure.

func (*Serializer) MessageBytesBuiltin

func (s *Serializer) MessageBytesBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) SymbolName deprecated

func (s *Serializer) SymbolName(v *lisp.LVal) (string, bool)

SymbolName returns the name of the symbol that v represents and the value true. If v does not represent a symbol SymbolName returns a false second argument

Deprecated: SymbolName is no longer used internally for serialization and should be avoided.

func (*Serializer) UseExactIntegersBuiltin added in v1.50.0

func (s *Serializer) UseExactIntegersBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

func (*Serializer) UseStringNumbersBuiltin

func (s *Serializer) UseStringNumbersBuiltin(env *lisp.LEnv, args *lisp.LVal) *lisp.LVal

Jump to

Keyboard shortcuts

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