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 ¶
- Variables
- func Clone(v any) any
- func Equal(a, b any) bool
- func Parse(data []byte) (any, error)
- func SameValue(a, b any) bool
- func StringArray(ss []string) []any
- func Stringify(v any, indent string) []byte
- func Strings(a []any) []string
- func TrimJS(s string) string
- func Truthy(v any) bool
- type Object
- func (o *Object) Array(k string) []any
- func (o *Object) Assign(src *Object)
- func (o *Object) Clear()
- func (o *Object) Copy() *Object
- func (o *Object) Delete(k string)
- func (o *Object) Get(k string) (any, bool)
- func (o *Object) Has(k string) bool
- func (o *Object) Keys() []string
- func (o *Object) Len() int
- func (o *Object) MarshalJSON() ([]byte, error)
- func (o *Object) Number(k string) (float64, bool)
- func (o *Object) Object(k string) *Object
- func (o *Object) Replace(src *Object)
- func (o *Object) Set(k string, v any)
- func (o *Object) String(k string) string
Constants ¶
This section is empty.
Variables ¶
var ErrSyntax = errors.New("jsvalue: invalid JSON")
ErrSyntax is any text JSON.parse refuses.
var Undefined = undefinedValue{}
Undefined given to Set removes the key, as JSON.stringify drops an undefined property.
Functions ¶
func Equal ¶
Equal says whether two values stringify alike (key order counts, as it does for JSON.stringify comparisons).
func Parse ¶
Parse reads JSON as JSON.parse does: a duplicate key keeps its first place and its last value, numbers are float64.
func SameValue ¶
SameValue compares as a deep equality that ignores key order (objects with the same keys and equal values).
func StringArray ¶
StringArray turns strings into an array 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 (*Object) Keys ¶
Keys in the order JavaScript enumerates them: array-index keys ascending, then the others in insertion order.
func (*Object) MarshalJSON ¶
MarshalJSON lets encoding/json write an object in its own key order.
func (*Object) Replace ¶
Replace makes o hold exactly what src holds, in src's order: the object a caller holds becomes the other one.