jsonfields

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package jsonfields finds the fields encoding/json reads from a JSON object into a Go struct. The schema package describes them and the strict decoder checks arguments against them, so a schema and its decoder agree on every name and on which fields are required.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	Name     string
	Tagged   bool  // the name came from a json tag
	Index    []int // the field's index path from the outer struct
	Field    reflect.StructField
	Type     reflect.Type // the field's type, with an unnamed pointer followed
	Optional bool         // ",omitempty" or ",omitzero"
	Quoted   bool         // ",string" on a string, number, or boolean field
	// ViaUnexportedPtr names the unexported struct type of an embedded pointer on the field's
	// path, if any: encoding/json cannot allocate it, so decoding the field fails.
	ViaUnexportedPtr string
}

Field is one field encoding/json reads from a JSON object into a struct.

func Of

func Of(t reflect.Type) ([]Field, error)

Of returns the fields encoding/json reads for struct type t, in index order. It is a port of encoding/json's typeFields: a breadth-first walk over embedded structs, then Go's dominance rules (the shallowest name wins, a json tag breaks a tie at one depth, and any other tie removes the name). A json tag name encoding/json does not accept is a *TagNameError.

func (Field) Required

func (f Field) Required() bool

Required reports whether the field is required: it is not a pointer, and its json tag has neither ",omitempty" nor ",omitzero". This is the one definition the schema's "required" list and the strict decoder's missing-field check share.

type TagNameError

type TagNameError struct {
	Struct reflect.Type
	Field  string // the Go field name
	Name   string // the tag name
}

TagNameError reports a json tag name encoding/json does not accept (one with a quote, a backslash, or another reserved character). encoding/json v1 falls back to the Go name there and its v2 implementation reads the name differently, so the field has no one JSON name.

func (*TagNameError) Error

func (e *TagNameError) Error() string

Error names the struct, the field and the rejected tag name.

Jump to

Keyboard shortcuts

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