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 ¶
const MaxDepth = 10000
MaxDepth bounds how deep Parse follows nesting, as encoding/json does.
Variables ¶
var ErrDepth = errors.New("jsjson: nesting too deep")
ErrDepth is returned for a value nested past MaxDepth.
Functions ¶
func Number2String ¶
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.
Types ¶
type String ¶
type String []uint16
String is a JavaScript string: UTF-16 code units.
func FromString ¶
FromString is s as UTF-16; invalid UTF-8 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 Parse ¶
Parse reads one JSON value, as JSON.parse does after a UTF-8 decode: an invalid byte becomes U+FFFD.
func (*Value) Set ¶
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) StringifyIndent ¶
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 {}.