jsonval

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatNumber

func FormatNumber(f float64) string

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 Index

func Index(obj Object, k string) int

Index returns the position of key k in obj, or -1.

func MarshalNoEscape

func MarshalNoEscape(v any) ([]byte, error)

MarshalNoEscape marshals v to compact JSON without escaping <, > and &, matching JavaScript's JSON.stringify.

func MarshalOMap

func MarshalOMap(o *OMap) ([]byte, error)

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

func ParseOrdered(data []byte) (any, error)

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

func Stringify(v any, indent int) string

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

func StringifyIndent(v any, unit string) string

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 NewOMap

func NewOMap() *OMap

NewOMap returns an empty ordered map.

func (*OMap) Get

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

Get returns the value stored under key, and whether it is present.

func (*OMap) Keys

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

Keys returns the keys in insertion order. The slice is the map's own; callers only range over it.

func (*OMap) MarshalJSON

func (o *OMap) MarshalJSON() ([]byte, error)

MarshalJSON emits the object with keys in insertion order.

func (*OMap) Merge

func (o *OMap) Merge(src *OMap)

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.

func (*OMap) Set

func (o *OMap) Set(key string, v any) *OMap

Set stores v under key and returns o for chaining. A new key goes to the end; an existing one keeps its position.

func (*OMap) Value

func (o *OMap) Value(key string) any

Value returns the value stored under key, or nil when it is absent, the way a Go map read returns the zero 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

func Buffer(b []byte) Object

Buffer renders a byte slice the way JSON.stringify renders a Node Buffer: {"type":"Buffer","data":[...]}.

func ESOrder

func ESOrder(obj Object) Object

ESOrder reorders an object's entries the way JavaScript enumerates own string keys (OrdinaryOwnPropertyKeys): integer-index keys first in ascending numeric order, then the remaining keys in insertion order. JSON.stringify and Object.keys both use this order.

type Pair

type Pair struct {
	K string
	V any
}

Pair is one ordered key/value entry of a Object.

type Undefined

type Undefined struct{}

Undefined represents a JavaScript `undefined`. JSON.stringify omits it from objects and renders it as null inside arrays; at the top level the whole output is empty. Only CBOR decoding produces it.

Jump to

Keyboard shortcuts

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