jv

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

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

ErrSyntax reports JSON text that JSON.parse rejects. Read-only: assigned once at initialization.

Functions

func Append

func Append(dst []byte, value any) []byte

Append appends JSON.stringify(value) to dst.

func AppendNumber

func AppendNumber(dst []byte, f float64) []byte

AppendNumber appends Number::toString of f (ECMA-262 6.1.6.1.20) for a finite f. Negative zero prints "0".

func AppendString

func AppendString(dst []byte, s string) []byte

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 Clone

func Clone(value any) any

Clone returns a deep copy.

func Concat

func Concat(a, b string) string

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

func Equal(a, b any) bool

Equal reports deep equality. Object key order is ignored, as in a deep comparison of the decoded values.

func FormatNumber

func FormatNumber(f float64) string

FormatNumber returns Number::toString of f for a finite f.

func FromUTF16

func FromUTF16(u []uint16) string

FromUTF16 returns the string of the code units.

func HasPrefix

func HasPrefix(s, p string) bool

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

func IsIndexKey(key string) bool

IsIndexKey reports whether key is a canonical ECMAScript array index: a decimal integer without leading zeros in 0..2^32-2.

func IsNonEmptyArray

func IsNonEmptyArray(text []byte) bool

IsNonEmptyArray reports whether text, the text of a JSON value that Parse accepts, is an array with at least one element.

func MemberText

func MemberText(text []byte, key string) []byte

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

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

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 ParseString

func ParseString(text string) (any, error)

ParseString is Parse for a string.

func PlainEnd

func PlainEnd(s string, i int) int

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 SliceFrom

func SliceFrom(s string, from int) string

SliceFrom returns s.slice(from) for 0 <= from.

func SplitArrayMembers

func SplitArrayMembers(text []byte) (members [][]Member, err error)

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.

func Stringify

func Stringify(value any) string

Stringify returns JSON.stringify(value) with no indent. A non-finite number prints as null, as JSON.stringify prints it. Any type outside the value model is a programming error and prints as null.

func UTF16Len

func UTF16Len(s string) int

UTF16Len returns s.length.

func UTF16Units

func UTF16Units(s string) []uint16

UTF16Units returns the code units of s.

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

func NewObjectCap(n int) *Object

NewObjectCap returns an empty object with room for n members.

func (*Object) Delete

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

Delete removes key and reports whether it was a member.

func (*Object) Entries

func (o *Object) Entries(fn func(key string, value any) bool)

Entries calls fn for each member in enumeration order until fn returns false.

func (*Object) Get

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

Get returns the member named key.

func (*Object) Has

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

Has reports whether key is a member.

func (*Object) Keys

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

Keys returns the keys in ECMAScript enumeration order. The result is shared with the object and must not be modified.

func (*Object) Len

func (o *Object) Len() int

Len returns the number of members.

func (*Object) Members

func (o *Object) Members(fn func(key string, value any) bool)

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.

func (*Object) Set

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

Set replaces the value of an existing member in place, or appends a new member. It matches assignment to an own data property.

func (*Object) Shallow

func (o *Object) Shallow() *Object

Shallow returns a copy that shares the member values.

type Raw

type Raw []byte

Raw is a pre-serialized JSON value: valid JSON text that Append splices verbatim. It lets a caller hand a large stored record (a model message) to a record without parsing it. Equal and Clone treat it as opaque bytes.

Jump to

Keyboard shortcuts

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