jsparity

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jsparity writes JSON exactly as JavaScript's JSON.stringify writes it. Its primitives are those of durable/core/jv, which the TinyGo Durable core builds; this package is their one entry point for the rest of PiG and adds the forms jv does not need (non-finite numbers, 64-bit integers, booleans, three-state members).

A string is a sequence of UTF-16 code units held as WTF-8: a lone surrogate is its three-byte generalized UTF-8 form, as jv defines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBool

func AppendBool(dst []byte, b bool) []byte

AppendBool appends JSON.stringify(b).

func AppendInt

func AppendInt(dst []byte, n int64) []byte

AppendInt appends JSON.stringify of the number JavaScript holds for n: n itself when |n| is at most 2^53-1, and otherwise the nearest double.

func AppendNumber

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

AppendNumber appends JSON.stringify(f): Number::toString for a finite value (shortest round trip; exponent form from 1e21 and below 1e-6; -0 prints 0) and null for NaN and the infinities.

func AppendOpt

func AppendOpt[T any](dst []byte, o Opt[T], appendValue func([]byte, T) []byte) []byte

AppendOpt appends the member's JSON: null when it is null, and appendValue's output when it is set. An absent member appends what JSON.stringify writes for undefined inside an array, null; an object writer checks Present and leaves the member out instead.

func AppendString

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

AppendString appends JSON.stringify(s) (ECMA-262 QuoteJSONString): the quote, the backslash and C0 controls are escaped (\b \f \n \r \t, other controls as \u00xx), a lone surrogate prints as \udxxx, a high surrogate directly followed by a low one prints as the character they form, and every other character prints as itself (no HTML escaping; U+2028, U+2029 and U+007F literal). A byte that is not WTF-8 prints as U+FFFD, the character a decoded JavaScript string would hold.

It is jv.AppendString, which finds runs that need no escape eight bytes at a time and copies them whole.

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). An object enumerates those keys first, in ascending order, before its other keys in insertion order.

Types

type Opt

type Opt[T any] struct {
	// contains filtered or unexported fields
}

Opt is an object member that JavaScript can leave undefined (JSON.stringify omits it), set to null, or set to a value.

func Null

func Null[T any]() Opt[T]

Null is a member set to null.

func Some

func Some[T any](v T) Opt[T]

Some is a member set to v.

func (Opt[T]) Get

func (o Opt[T]) Get() (T, bool)

Get returns the value and whether the member is set to one. An absent or null member returns the zero value and false.

func (Opt[T]) IsNull

func (o Opt[T]) IsNull() bool

IsNull reports whether the member is null.

func (Opt[T]) Present

func (o Opt[T]) Present() bool

Present reports whether JSON.stringify writes the member: it is null or set.

Jump to

Keyboard shortcuts

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