jsonx

package
v1.34.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 2 modules, 3 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 provides helpers for json in/out operations

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrObjectExpected is returned when a JSON round-trip does not produce an object
	ErrObjectExpected = errors.New("json value is not an object")
	// ErrKeyRequired is returned when a JSON object key is empty
	ErrKeyRequired = errors.New("json key is required")
)
View Source
var Reflector = &jsonschema.Reflector{
	AllowAdditionalProperties:  false,
	RequiredFromJSONSchemaTags: true,
}

Reflector is the shared JSON schema reflector used by SchemaFrom and SchemaID.

Functions

func ApplyOverlay added in v1.15.0

func ApplyOverlay[T any](base T, overlay any) (T, error)

ApplyOverlay applies a JSON overlay to an existing typed value

func CloneRawMessage added in v1.15.0

func CloneRawMessage(raw json.RawMessage) json.RawMessage

CloneRawMessage copies a raw JSON document to avoid accidental aliasing

func Decode added in v1.27.11

func Decode[T any](raw json.RawMessage) (T, error)

Decode unmarshals a json.RawMessage into a typed value

func DecodeAnyOrNil added in v1.15.0

func DecodeAnyOrNil(raw json.RawMessage) any

DecodeAnyOrNil decodes raw JSON to an untyped value or returns nil on failure/empty input

func DeepMerge added in v1.15.0

func DeepMerge(base, patch json.RawMessage) (json.RawMessage, bool, error)

DeepMerge deep-merges patch into base and reports whether the document changed. Both arguments must be JSON objects. Returns the merged document and a boolean indicating whether the result differs from base

func InjectDefaults added in v1.21.0

func InjectDefaults(schema json.RawMessage, defaults map[string]any) (json.RawMessage, error)

InjectDefaults returns the schema document as raw JSON with stored values injected as "default" at every level of nesting, following $ref pointers into $defs recursively. Using jsonschema.Schema preserves the original property ordering on serialization.

func IsEmptyRawMessage added in v1.15.0

func IsEmptyRawMessage(raw json.RawMessage) bool

IsEmptyRawMessage reports whether a raw JSON message is empty or null

func MergeObjectMap added in v1.15.0

func MergeObjectMap(base json.RawMessage, patch map[string]json.RawMessage) (json.RawMessage, bool, error)

MergeObjectMap shallow-merges a raw JSON object with the supplied top-level patch map

func PropertyNames added in v1.21.0

func PropertyNames[T any]() []string

PropertyNames reflects a Go type and returns the top-level JSON property names from the generated JSON schema. Properties from embedded structs are promoted by the reflector and appear as top-level names.

func RoundTrip

func RoundTrip(input any, output any) error

RoundTrip marshals input to JSON and unmarshals it into output

func SchemaFrom added in v1.21.0

func SchemaFrom[T any]() json.RawMessage

SchemaFrom reflects a JSON schema from a Go type and returns it as raw JSON.

func SchemaID added in v1.21.0

func SchemaID(schema json.RawMessage) string

SchemaID extracts the definition key from a reflected JSON schema's $ref path.

func SetObjectKey added in v1.15.0

func SetObjectKey(base json.RawMessage, key string, value any) (json.RawMessage, bool, error)

SetObjectKey sets or replaces one top-level key in a raw JSON object

func ToMap

func ToMap(value any) (map[string]any, error)

ToMap converts an arbitrary value into a JSON object map

func ToRawMap added in v1.15.0

func ToRawMap(value any) (map[string]json.RawMessage, error)

ToRawMap converts an arbitrary value into a JSON object map of raw values.

func ToRawMessage added in v1.15.0

func ToRawMessage(value any) (json.RawMessage, error)

ToRawMessage converts an arbitrary value into a raw JSON document

func UnmarshalIfPresent added in v1.15.0

func UnmarshalIfPresent(raw json.RawMessage, output any) error

UnmarshalIfPresent unmarshals raw JSON when it is non-empty

func ValidateSchema added in v1.15.0

func ValidateSchema(schema any, document any) (*gojsonschema.Result, error)

ValidateSchema validates a JSON document against a JSON schema and returns the raw gojsonschema result for caller-specific error handling.

func ValidationErrorStrings added in v1.15.0

func ValidationErrorStrings(result *gojsonschema.Result) []string

ValidationErrorStrings converts schema validation errors into string messages.

Types

type PropertyDescriptor added in v1.21.0

type PropertyDescriptor struct {
	// Name is the JSON property key as it appears in the reflected schema.
	Name string
	// Description is the human-readable description extracted from the jsonschema description tag.
	Description string
}

PropertyDescriptor is a top-level JSON schema property with its name and description.

func PropertyDescriptors added in v1.21.0

func PropertyDescriptors[T any]() []PropertyDescriptor

PropertyDescriptors reflects a Go type and returns its top-level JSON properties with names and descriptions from the generated JSON schema. Properties from embedded structs are promoted by the reflector and appear as top-level entries.

Jump to

Keyboard shortcuts

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