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

jsonfield

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package jsonfield reads the fields of a struct type the way encoding/json marshals them, for the declarations of the root package and the generators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByName

func ByName(structType reflect.Type) map[string]reflect.StructField

ByName is Of as a map from the JSON key to the struct field.

func Name

func Name(field reflect.StructField) (name string, tagged, included bool)

Name is the field's JSON name, whether the tag names it and whether encoding/json marshals the field at all (`json:"-"` does not). A tag with options only (`json:",omitempty"`) does not name the field. The name is read back from encoding/json itself (a one-field struct with the field's tag is marshaled), so it follows the toolchain's own tag rules: which names it accepts, where it cuts an odd one, when it falls back to the Go name.

Types

type Field

type Field struct {
	Name        string
	StructField reflect.StructField
	OmitEmpty   bool
	OmitZero    bool
	Quoted      bool
}

Field is one field encoding/json marshals: its JSON key, the struct field (its Index the path from the outer type, as reflect's FieldByIndex takes it) and the tag's options. OmitEmpty and OmitZero are the `omitempty` and `omitzero` options; Quoted is the `string` option on a field it applies to (a bool, number or string, or a pointer to one), whose value is written inside a JSON string.

func Of

func Of(structType reflect.Type) []Field

Of returns the fields encoding/json marshals for a struct type, in its order (by index path), following its typeFields: one embedding level at a time, a type visited once (the shallowest occurrence), a type embedded twice at one level cancels its fields, embedded structs (by value or pointer) flatten unless their tag names them, a tagged field beats untagged ones at the same depth, and a name that stays ambiguous is dropped. A type that is not a struct has no fields.

Jump to

Keyboard shortcuts

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