jsjson

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jsjson holds a JSON value as JavaScript holds what JSON.parse returns, and writes it as JSON.stringify does, for a reader that must reproduce a JavaScript program's own bytes: a hash over its serialisation, or the text it sends. Strings are UTF-16 code units, so a lone surrogate an escape wrote survives; numbers are doubles, written as Number.prototype.toString writes them; an object keeps its members in JavaScript's order, the array-index keys first in ascending order and then the rest in the order first written, a repeated member keeping its first place and its last value. It imports the standard library alone.

Index

Constants

View Source
const MaxDepth = 10000

MaxDepth bounds how deep Parse follows nesting, as encoding/json does.

Variables

View Source
var ErrDepth = errors.New("jsjson: nesting too deep")

ErrDepth is returned for a value nested past MaxDepth.

Functions

func Number2String

func Number2String(f float64) string

Number2String writes f as Number.prototype.toString does, and as JSON.stringify does: the shortest digits that read back as f, in fixed notation from 1e-6 up to 1e21 and in exponent notation outside it; NaN and the infinities, which JSON cannot hold, as null.

func Quote

func Quote(b *bytes.Buffer, s String)

Quote writes s as JSON.stringify writes a string: '"' and '\\' escaped, the control characters as \b, \f, \n, \r, \t or \u00xx, a lone surrogate as \udxxx, everything else as itself.

func QuoteString

func QuoteString(s string) string

QuoteString is Quote of a Go string.

Types

type Kind

type Kind uint8

Kind is a value's JSON type.

const (
	Null Kind = iota
	Bool
	Number
	Str
	Array
	Object
)

The kinds.

type String

type String []uint16

String is a JavaScript string: UTF-16 code units.

func FromString

func FromString(s string) String

FromString is s as UTF-16; invalid UTF-8 becomes U+FFFD.

func (String) Lone

func (s String) Lone() bool

Lone reports whether s holds a surrogate that is not half of a pair, which a Go string cannot hold.

func (String) String

func (s String) String() string

String is s as UTF-8; a lone surrogate becomes U+FFFD.

type Value

type Value struct {
	Kind Kind
	Bool bool
	Num  float64
	Str  String
	// Items are an array's elements.
	Items []*Value
	// Keys and Vals are an object's members, in JavaScript's order.
	Keys []String
	Vals []*Value
}

Value is a JSON value as JSON.parse returns it.

func NewString

func NewString(s string) *Value

NewString is a string value.

func Parse

func Parse(data []byte) (*Value, error)

Parse reads one JSON value, as JSON.parse does after a UTF-8 decode: an invalid byte becomes U+FFFD.

func (*Value) Clone

func (v *Value) Clone() *Value

Clone is a deep copy of v.

func (*Value) Delete

func (v *Value) Delete(key string)

Delete removes an object's member.

func (*Value) Get

func (v *Value) Get(key string) *Value

Get returns an object's member, nil when v is no object or has none.

func (*Value) Set

func (v *Value) Set(key string, val *Value)

Set sets an object's member as an assignment does: an existing member keeps its place, a new one goes where JavaScript's order puts it.

func (*Value) Stringify

func (v *Value) Stringify() []byte

Stringify writes v as JSON.stringify(v) does, with no indentation.

func (*Value) StringifyIndent

func (v *Value) StringifyIndent() []byte

StringifyIndent writes v as JSON.stringify(v, null, 2) does: each member and element on a line of its own, indented two spaces a level, an empty array or object as [] or {}.

func (*Value) Text

func (v *Value) Text() (string, bool)

Text is the string a value holds, and whether it is a string.

Jump to

Keyboard shortcuts

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