Help us shape the future of Go! Take the 2026 Go Developer Survey

jsonvalue

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package jsonvalue renders a decoded JSON value for a human-readable diagnostic.

Two plan-time validators check an author's JSON against a declared schema and have to say, in a sentence, what they found where they expected something else: the SDK's appleschemas/profiles against Apple's payload key table, and internal/common/aischemas against a vendor's JSON Schema. The schema languages share nothing, but the vocabulary for naming a decoded value does — and getting it wrong is how a diagnostic ends up reading "expected a integer, found 1e+09".

Everything here operates on the output of encoding/json, so a number arrives as float64 unless the decoder was set to use json.Number; both are handled.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Article

func Article(name string) string

Article prefixes a declared type name for readability in a sentence.

The vowel set deliberately omits "u": every name this is called with is a JSON or Apple payload type (object, array, integer, number, string, boolean, null, dictionary, data, date, real, any), none of which begins with u, and "a unit"-shaped words would take "a" rather than "an" anyway.

func Describe

func Describe(value any) string

Describe names a value's JSON type for a diagnostic, with its article, so it drops into a sentence as "expected a string, found an array".

func FormatNumber

func FormatNumber(number float64) string

FormatNumber renders a JSON number without exponent notation, so a large integer reads as the digits the author wrote.

The int64 path is taken only inside int64's range, and that bound is this function's own concern rather than IsWhole's: converting an out-of-range float to int64 is implementation-defined in Go, so 1e30 would otherwise print as int64's minimum instead of its own digits. The comparison is strict against MaxInt64 because the constant rounds up to 2^63 as a float64, which does not fit.

func IsWhole

func IsWhole(number float64) bool

IsWhole reports whether a JSON number has no fractional part. Integer-ness is a property of the value, not of any machine type it might be squeezed into, so 1e30 is as whole as 3 — which is why this deliberately carries no int64 bound and every caller that converts to int64 checks that bound itself. The round-trip comparison this replaces, `number == float64(int64(number))`, folded the two questions together and so called 1e30 fractional on amd64, where an out-of-range float-to-int64 conversion is implementation-defined: a schema declaring `"type":"integer"` then reported 1e30 as the self-contradictory "expected an integer, found a whole number".

func Numeric

func Numeric(value any) (float64, bool)

Numeric reports whether a value is a JSON number and returns it as a float64.

func Render

func Render(value any) string

Render quotes a value the way an author would recognise it: a string in quotes, a number as its digits, anything else by its type. Suits naming the offending value in a diagnostic without spilling a whole nested object into it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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