Documentation
¶
Overview ¶
Package jsonval holds an insertion-ordered JSON value model.
Go's encoding/json stores an object in a map, which loses the order its keys arrived in. Several operations have to preserve that order — a formatter must not reshuffle the document it was given, and AMF encodes keys in the order they appear — so Object keeps its pairs in a slice instead.
Stringify writes a value back out the way JavaScript's JSON.stringify does, including its rule that integer-like keys come first in ascending order and everything else follows in insertion order. ESOrder applies that rule on its own.
Index ¶
- func FormatNumber(f float64) string
- func Index(obj Object, k string) int
- func MarshalNoEscape(v any) ([]byte, error)
- func MarshalOMap(o *OMap) ([]byte, error)
- func ParseOrdered(data []byte) (any, error)
- func Stringify(v any, indent int) string
- func StringifyIndent(v any, unit string) string
- type OMap
- type Object
- type Pair
- type Undefined
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatNumber ¶
FormatNumber matches JSON.stringify's number output: NaN/Infinity become null, negative zero becomes "0", and everything else uses Go's ECMAScript- compatible float formatting.
func MarshalNoEscape ¶
MarshalNoEscape marshals v to compact JSON without escaping <, > and &, matching JavaScript's JSON.stringify.
func MarshalOMap ¶
MarshalOMap renders an omap as compact JSON text (CyberChef's minified form). The packet parsers only store ints, strings and nested omaps, all JSON-safe, so their `MarshalOMap` error propagations are unreachable in practice; the encoder's actual failure mode is exercised directly in parsenet_test.go.
func ParseOrdered ¶
ParseOrdered parses JSON preserving object key order (as Object), which Go's map-based json.Unmarshal cannot. Numbers become float64 (matching a JavaScript JSON.parse), objects Object, arrays []any, and duplicate object keys keep their first position with the last value (JS object semantics). It rejects trailing data after the single top-level value.
func Stringify ¶
Stringify reproduces JavaScript's JSON.stringify(value, null, indent): indent 0 is compact, indent > 0 pretty-prints with that many spaces. It understands nil, bool, int64, float64, string, []any, Object and Undefined values. Stringify serialises v like JSON.stringify(value, null, indent) using indent spaces (0 = compact).
func StringifyIndent ¶
StringifyIndent serialises v like JSON.stringify(value, null, unit), where unit is the literal indentation string (a tab, N spaces, or any string; "" produces compact output). This backs JSON Beautify's binaryShortString indent.
Types ¶
type OMap ¶
type OMap struct {
// contains filtered or unexported fields
}
OMap is an insertion-ordered JSON object. Go's encoding/json sorts map keys, so the packet parsers build omaps to preserve CyberChef's field order.
func (*OMap) Keys ¶
Keys returns the keys in insertion order. The slice is the map's own; callers only range over it.
func (*OMap) MarshalJSON ¶
MarshalJSON emits the object with keys in insertion order.
func (*OMap) Merge ¶
Merge appends all of src's entries into o, in src's order. A key both maps hold keeps its position in o and takes src's value.
type Object ¶
type Object []Pair
Object is an insertion-ordered JSON object, used where key order must be preserved (Avro records/maps, CBOR maps) rather than sorted as Go's map marshalling would do.
func Buffer ¶
Buffer renders a byte slice the way JSON.stringify renders a Node Buffer: {"type":"Buffer","data":[...]}.