jsonx

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT, MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package jsonx is JSON.stringify.

Every byte this program emits as JSON goes through here, because three of those byte sequences are signed or stored and replayed (the share JWT, the webhook envelope, a delivery's payload), every API response is two-space pretty-printed, and a Go encoder left to its defaults does three things JavaScript does not: sorts map keys, HTML-escapes `<>&`, and appends a newline. So: structs (field order) or *omap.Map (insertion order), never map[string]any; SetEscapeHTML(false); the trailing newline trimmed. Numbers are emitted the way JSON.stringify emits them — an integer is an integer, a float is the shortest round-trippable form — which is what encoding/json does for int64/float64 when the value has the right static type. Keep it typed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(b bool) *bool

Bool, Str and Int are the pointer helpers for the tri-state and nullable-not-absent fields: `*bool` without omitempty marshals `null` when nil and `false` when false — the distinction invariant 11 exists to keep.

func Int

func Int(i int64) *int64

Int returns a pointer to i.

func Marshal

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

Marshal is JSON.stringify(v).

func MarshalIndent

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

MarshalIndent is JSON.stringify(v, null, 2).

func Must

func Must(v any) []byte

Must is Marshal for values that cannot fail (structs of primitives). Panics otherwise.

func Number

func Number(f float64) string

Number renders a float64 the way JavaScript's Number.prototype.toString does (which is also how JSON.stringify renders it): integers without a fraction, otherwise the shortest representation that round-trips, with exponent notation outside [1e-7, 1e21). NaN and ±Infinity are "null" in JSON and "NaN"/"Infinity" in String() — callers that need the latter use NumberString.

func NumberString

func NumberString(f float64) string

NumberString is String(number) in JavaScript.

func Str

func Str(s string) *string

Str returns a pointer to s.

Types

type KV

type KV struct {
	K string
	V any
}

KV is one pair of an Object.

type Object

type Object []KV

Object is an ordered list of key/value pairs that marshals in order — for ad-hoc response bodies built at a route, where a struct would be ceremony. Prefer structs for anything reused.

func O

func O(pairs ...any) Object

O builds an Object from alternating keys and values: O("a", 1, "b", "x"). The shape every event payload takes — ordered like the TS object literal it replaces, without a struct per event.

func (Object) MarshalJSON

func (o Object) MarshalJSON() ([]byte, error)

MarshalJSON emits the pairs in order.

Jump to

Keyboard shortcuts

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