Documentation
¶
Overview ¶
Package jv is the ordered JSON value model of the Durable core's session layer.
A value is nil, bool, float64, string, *Object or []any. An Object keeps insertion order and enumerates in ECMAScript property order (canonical array index keys ascending first, then the other keys in insertion order), so a value stringifies exactly as JSON.stringify prints the object JSON.parse built. A string holds well-formed UTF-8 except that a lone UTF-16 surrogate is held as its three-byte generalized encoding (WTF-8), because JSON text can carry one and JSON.stringify prints it back as a \udXXX escape.
The package uses no reflection and no encoding/json, and keeps no package-level mutable state.
Index ¶
- Variables
- func Append(dst []byte, value any) []byte
- func AppendNumber(dst []byte, f float64) []byte
- func AppendString(dst []byte, s string) []byte
- func Clone(value any) any
- func Concat(a, b string) string
- func Equal(a, b any) bool
- func FormatNumber(f float64) string
- func FromUTF16(u []uint16) string
- func HasPrefix(s, p string) bool
- func IsIndexKey(key string) bool
- func IsNonEmptyArray(text []byte) bool
- func MemberText(text []byte, key string) []byte
- func Parse(text []byte) (any, error)
- func ParseString(text string) (any, error)
- func PlainEnd(s string, i int) int
- func SliceFrom(s string, from int) string
- func SplitArrayMembers(text []byte) (members [][]Member, err error)
- func Stringify(value any) string
- func UTF16Len(s string) int
- func UTF16Units(s string) []uint16
- type Member
- type Object
- func (o *Object) Delete(key string) bool
- func (o *Object) Entries(fn func(key string, value any) bool)
- func (o *Object) Get(key string) (any, bool)
- func (o *Object) Has(key string) bool
- func (o *Object) Keys() []string
- func (o *Object) Len() int
- func (o *Object) Members(fn func(key string, value any) bool)
- func (o *Object) Set(key string, value any)
- func (o *Object) Shallow() *Object
- type Raw
Constants ¶
This section is empty.
Variables ¶
var ErrNotArray = errors.New("JSON is not an array")
ErrNotArray reports JSON text that Parse accepts but whose top-level value is not an array. Read-only: assigned once at initialization.
var ErrSyntax = errors.New("invalid JSON")
ErrSyntax reports JSON text that JSON.parse rejects. Read-only: assigned once at initialization.
Functions ¶
func AppendNumber ¶
AppendNumber appends Number::toString of f (ECMA-262 6.1.6.1.20) for a finite f. Negative zero prints "0".
func AppendString ¶
AppendString appends the JSON.stringify form of s: only the quote, the backslash and C0 controls are escaped, an unpaired surrogate prints as \udXXX, a high surrogate directly followed by a low one prints as the character the pair forms (as JavaScript joins them), and every other character prints as itself.
Runs that need no escape are found eight bytes at a time and copied whole, and dst grows once for the whole string.
func Concat ¶
Concat returns a + b, joining a trailing high surrogate of a with a leading low surrogate of b into one code point as UTF-16 concatenation does.
func Equal ¶
Equal reports deep equality. Object key order is ignored, as in a deep comparison of the decoded values.
func FormatNumber ¶
FormatNumber returns Number::toString of f for a finite f.
func HasPrefix ¶
HasPrefix reports whether s starts with p as a UTF-16 sequence. A prefix ending in a lone high surrogate matches the first half of a pair in s.
func IsIndexKey ¶
IsIndexKey reports whether key is a canonical ECMAScript array index: a decimal integer without leading zeros in 0..2^32-2.
func IsNonEmptyArray ¶
IsNonEmptyArray reports whether text, the text of a JSON value that Parse accepts, is an array with at least one element.
func MemberText ¶
MemberText returns the text of the value of the member named key of the object text, the last member of the name as Parse keeps it, or nil when there is none. text must be an object that Parse accepts, such as the text of a member that SplitArrayMembers reported: MemberText does not validate it, it only walks past each value. It does not panic on other text and returns whatever it reads.
func Parse ¶
Parse returns JSON.parse(text). Duplicate object keys keep the position of the first occurrence and the value of the last. A number outside the float64 range parses to +Inf or -Inf, as in JavaScript.
func PlainEnd ¶
PlainEnd is the index of the first byte at or after i that a JSON string scan must look at (below 0x20, '"', '\\' or from 0x80), or len(s). AppendString and the ai package's chunk decoder share it. It reads eight bytes at a time with byte loads and shifts (no unsafe, so TinyGo builds it the same way): lane k of the word is byte k, and a borrow can mark a lane above a marked one falsely, never one below, so the lowest mark is exact.
func SplitArrayMembers ¶
SplitArrayMembers reports, for each element of the array text in order, the members of the element when it is an object, and nil when it is another kind of value, without building the values. It accepts exactly the texts Parse accepts and fails with ErrSyntax on the others; a text that Parse accepts as another kind of value fails with ErrNotArray. A member's key is the key as Parse decodes it, and its text is valid JSON that Parse reads as its value. Members are reported in the order they are written, with a repeated key reported each time: Parse keeps the last value of a repeated key, so a caller that reads one key takes the last member that has it.
Types ¶
type Member ¶
type Member struct {
Key string
Raw []byte // the value's text, without surrounding white space; it aliases the scanned text
}
Member is one member of a JSON object text: its key as Parse decodes it, and the raw JSON text of its value.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object is a JSON object with ECMAScript key order.
func NewObject ¶
func NewObject() *Object
NewObject returns an empty object with room for its first few members.
func NewObjectCap ¶
NewObjectCap returns an empty object with room for n members.
func (*Object) Entries ¶
Entries calls fn for each member in enumeration order until fn returns false.
func (*Object) Keys ¶
Keys returns the keys in ECMAScript enumeration order. The result is shared with the object and must not be modified.
func (*Object) Members ¶
Members calls fn for each member in insertion order, index keys not first, until fn returns false. fn may Set a key that is already a member.