jsvalue

package
v0.16.397 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0 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

Overview

Package jsvalue holds JSON the way JavaScript does: objects keep their key order and every key they were given, so a document read, changed and written back carries the fields nobody here models, in the order the previous runtime wrote them, and Stringify writes what JSON.stringify writes.

Values are nil (null), bool, float64, string, []any and *Object. Undefined is not a value: setting it removes the key.

Index

Constants

This section is empty.

Variables

View Source
var ErrSyntax = errors.New("jsvalue: invalid JSON")

ErrSyntax is any text JSON.parse refuses.

View Source
var Undefined = undefinedValue{}

Undefined given to Set removes the key, as JSON.stringify drops an undefined property.

Functions

func Clone

func Clone(v any) any

Clone is a deep copy (structuredClone of JSON data).

func Equal

func Equal(a, b any) bool

Equal says whether two values stringify alike (key order counts, as it does for JSON.stringify comparisons).

func Parse

func Parse(data []byte) (any, error)

Parse reads JSON as JSON.parse does: a duplicate key keeps its first place and its last value, numbers are float64.

func SameValue

func SameValue(a, b any) bool

SameValue compares as a deep equality that ignores key order (objects with the same keys and equal values).

func StringArray

func StringArray(ss []string) []any

StringArray turns strings into an array value.

func Stringify

func Stringify(v any, indent string) []byte

Stringify writes v as JSON.stringify(v, null, indent) does; indent "" is the compact form.

func Strings

func Strings(a []any) []string

Strings is an array's string elements, in order.

func TrimJS

func TrimJS(s string) string

TrimJS trims as String.prototype.trim.

func Truthy

func Truthy(v any) bool

Truthy is JavaScript truthiness of a JSON value.

Types

type Object

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

Object is a JavaScript object with string keys.

The keys and values are two parallel slices; a small object (nearly every transcript entry) is searched linearly, which costs a fraction of the memory a map per object does, and only a wide one gets an index.

func NewObject

func NewObject(pairs ...any) *Object

NewObject is an empty object; pairs are set in order (key, value, key, value…).

func (*Object) Array

func (o *Object) Array(k string) []any

Array is the value of a key when it is an array.

func (*Object) Assign

func (o *Object) Assign(src *Object)

Assign copies every key of src onto o, as Object.assign.

func (*Object) Clear

func (o *Object) Clear()

Clear removes every key.

func (*Object) Copy

func (o *Object) Copy() *Object

Copy is a shallow copy ({...o}).

func (*Object) Delete

func (o *Object) Delete(k string)

Delete removes a key.

func (*Object) Get

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

Get is the value of a key; false when the key is absent.

func (*Object) Has

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

Has says whether the key is present.

func (*Object) Keys

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

Keys in the order JavaScript enumerates them: array-index keys ascending, then the others in insertion order.

func (*Object) Len

func (o *Object) Len() int

Len is the number of keys.

func (*Object) MarshalJSON

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

MarshalJSON lets encoding/json write an object in its own key order.

func (*Object) Number

func (o *Object) Number(k string) (float64, bool)

Number is the value of a key when it is a number.

func (*Object) Object

func (o *Object) Object(k string) *Object

Object is the value of a key when it is an object.

func (*Object) Replace

func (o *Object) Replace(src *Object)

Replace makes o hold exactly what src holds, in src's order: the object a caller holds becomes the other one.

func (*Object) Set

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

Set assigns a key: an existing one keeps its place, a new one goes last. Undefined removes it.

func (*Object) String

func (o *Object) String(k string) string

String is the value of a key when it is a string.

Jump to

Keyboard shortcuts

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