jsonutil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotObject = errors.New("json: value is not an object")

Reported by DecodeObject and UnmarshalJSON when the input holds a JSON value other than an object

Functions

func Append

func Append(dst []byte, v any, opts ...Option) ([]byte, error)

dst with the JSON text of v appended as Encode writes it

func AsMap

func AsMap(v any) (map[string]any, bool)

The plain map v holds: a map[string]any itself, an *Object's keys and values in a new map; false for anything else

func Clone

func Clone(v any) any

Deep copy of a decoded value: every *Object, map[string]any and []any copied at any depth, other values as they are

func Decode

func Decode[T ~string | ~[]byte](data T) (any, error)

Value of the JSON text in data: objects as *Object in document order, arrays as []any, integers that fit as int64, other numbers as float64, NaN and Infinity accepted, strings made valid UTF-8, a byte order mark and UTF-16 or UTF-32 input understood, anything after the value refused

func DecodePartial

func DecodePartial[T ~string | ~[]byte](data T) (any, error)

Value of UTF-8 JSON text cut off anywhere, as streamed tool arguments arrive: open arrays and objects closed, a member or element the text ends inside (an unfinished string, key or literal, a number still missing digits after its sign, point or exponent) left out, a number cut between digits kept as read; shapes as Decode gives them

func DecodePlain

func DecodePlain[T ~string | ~[]byte](data T) (any, error)

Value of the JSON text in data in the shapes encoding/json gives an any, objects as map[string]any and every number as float64, otherwise read as Decode reads it

func DecodePrefix

func DecodePrefix[T ~string | ~[]byte](data T) (any, int, error)

First JSON value in the UTF-8 text data after any leading space, in the shapes Decode gives, and the byte offset in data just past it; what follows is not looked at

func DecodeWith

func DecodeWith[T ~string | ~[]byte](data T, opts ...DecodeOption) (any, error)

Value of the JSON text in data as Decode reads it under the given options

func Encode

func Encode(v any, opts ...Option) (string, error)

JSON text of v: compact, UTF-8, no HTML escaping, *Object keys in insertion order, map keys sorted, nil slices and maps as [] and {}, every float with a fraction or exponent (1.0, never 1) so it decodes back as a float, NaN and infinities as null, structs by their json tags, json.Marshaler and encoding.TextMarshaler honored, errors as their text

func Ordered

func Ordered(v any) any

Copy of a value with every string-keyed map at any depth turned into an *Object with its keys sorted and every slice or array whose elements are not bytes into []any, other values as they are

func Plain

func Plain(v any) any

Copy of a decoded value with every *Object turned into a map[string]any at any depth, maps and slices copied, other values as they are

func Text

func Text(data []byte) (string, error)

UTF-8 text of JSON bytes written in UTF-8, UTF-16 or UTF-32, a byte order mark dropped; on bad input the text with U+FFFD in place of each bad sequence and a *SyntaxError naming the first

func Validate

func Validate[T ~string | ~[]byte](data T) error

Nil when data is one JSON text as Decode reads it, checked at any nesting depth without building values; otherwise a *SyntaxError that places a trailing comma at the comma, a string the input ends inside at its opening quote, and says at the end of the input what was expected there

Types

type DecodeOption

type DecodeOption func(*decoder)

Setting for DecodeWith

func BigInts

func BigInts() DecodeOption

Integers past the int64 range decoded exactly as *big.Int instead of rounded to float64

func ControlChars

func ControlChars() DecodeOption

Raw control characters inside strings taken as they are instead of refused

func UniqueKeys

func UniqueKeys() DecodeOption

A key repeated inside one object refused instead of its last value kept at its first position

type Object

type Object struct {
	// contains filtered or unexported fields
}

JSON object that keeps its keys in insertion order, a key set again keeping its first position

func AsObject

func AsObject(v any) (*Object, bool)

The object v holds: an *Object itself, a map[string]any copied into a new object with its keys sorted; false for anything else

func DecodeObject

func DecodeObject[T ~string | ~[]byte](data T) (*Object, error)

Object of the JSON text in data as Decode reads it, ErrNotObject wrapped when the text holds another kind of value

func NewObject

func NewObject() *Object

Empty object ready for Set

func ObjectOf

func ObjectOf(pairs ...any) *Object

Object of alternating string keys and values in the order given; panics on an odd count or a key that is not a string

func (*Object) All

func (o *Object) All() iter.Seq2[string, any]

Keys and values in insertion order over the keys present when the iteration started: one deleted on the way is still visited, one added is not

func (*Object) Clone

func (o *Object) Clone() *Object

Shallow copy with the same key order, nil for a nil object

func (*Object) Delete

func (o *Object) Delete(key string) bool

Removes key, reporting whether it was present; slices Keys returned earlier keep their contents, and an iteration of All in progress goes on over the keys it started with

func (*Object) Get

func (o *Object) Get(key string) (any, bool)

Value stored under key and whether the key is present

func (*Object) Has

func (o *Object) Has(key string) bool

Whether key is present

func (*Object) Keys

func (o *Object) Keys() []string

Keys in insertion order; the slice belongs to the object and must not be modified

func (*Object) Len

func (o *Object) Len() int

Number of keys

func (*Object) Map

func (o *Object) Map() map[string]any

Keys and values as a plain map, a shallow copy; nil for a nil object

func (*Object) MarshalJSON

func (o *Object) MarshalJSON() ([]byte, error)

Compact JSON of the object with its keys in insertion order

func (*Object) Merge

func (o *Object) Merge(other *Object)

Sets every key of other in other's order, keys already present keeping their position

func (*Object) Rename

func (o *Object) Rename(from, to string) bool

Moves the value under from to the key to at the same position; false when from is absent or to is already present

func (*Object) Set

func (o *Object) Set(key string, v any)

Stores v under key, a new key going last and an existing one keeping its position

func (*Object) String

func (o *Object) String() string

Compact JSON text of the object, values with no JSON form written as their fmt text and a failure as %!(error)

func (*Object) Take

func (o *Object) Take(key string) (any, bool)

Removes key and gives back the value it held and whether it was present

func (*Object) UnmarshalJSON

func (o *Object) UnmarshalJSON(data []byte) error

Replaces the contents with the JSON object in data, leaving them as they are for JSON null

func (*Object) Value

func (o *Object) Value(key string) any

Value stored under key, nil when the key is absent

func (*Object) ValueOr

func (o *Object) ValueOr(key string, def any) any

Value stored under key, def when the key is absent

type Option

type Option func(*encoder)

Setting for Encode and Append

func ASCII

func ASCII() Option

Every character outside printable ASCII that is not escaped anyway (DEL and everything past it) written as a \u escape, astral ones as a surrogate pair

func Convert

func Convert(fn func(any) (any, error)) Option

Hook for values with no JSON form (functions, channels, complex numbers, maps with unusable keys): fn's result is encoded in their place

func Indent

func Indent(n int) Option

Each array element and object member on its own line, indented n spaces a level and a space after each colon; zero puts them on their own lines without indentation

func SortKeys

func SortKeys() Option

Keys of every *Object written in sorted order instead of insertion order

func Spaced

func Spaced() Option

A space after each comma between items and after each colon on a single line, the layout some signature schemes recompute; Indent takes precedence

type SyntaxError

type SyntaxError struct {
	// What is wrong, without the position
	Msg string
	// Byte offset of the problem in the UTF-8 text read: past a leading byte order mark, and in the transcoded text for UTF-16 or UTF-32 input
	Offset int
	// Line of the problem, counted from 1
	Line int
	// Column of the problem in characters, counted from 1
	Column int
}

Malformed JSON input with the place it was found

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

The message with its line and column

Jump to

Keyboard shortcuts

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