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

jsonfields

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package jsonfields finds the JSON object members of a Go struct by the rules of encoding/json/v2: the fields which a value of the struct is marshaled to and unmarshaled from, with the options of their `json` tags.

A Go struct is a list of fields found by a breadth-first search over its fields and the fields of the structs embedded in it. A field hides the fields of the same name deeper than it, and of two fields at the same depth the one whose name is given by its tag. An error in the definition of the struct is reported when a value of it is marshaled or unmarshaled, not when the type is seen: an empty map of it is still {}.

Index

Constants

This section is empty.

Variables

View Source
var (
	// RawValueType is jsontext.Value, which is an embedded fallback.
	RawValueType reflect.Type
	// MethodTypes are the interfaces of the methods of marshaling and unmarshaling: a type which has one of them
	// can't be embedded.
	MethodTypes []reflect.Type
)

The types which the json packages give this package: the ones it can't import.

Functions

func FoldName

func FoldName(name []byte) []byte

FoldName returns the name of which the names that case:ignore matches are the same: the case is folded, and '_' and '-' are removed.

Types

type Casing

type Casing int8

Casing is how the name of a field is matched when unmarshaling, by its `case` option.

const (
	CaseIgnore Casing = 1 << iota // case:ignore, which ignores the case, '-' and '_'
	CaseStrict                    // case:strict
)

type Error

type Error struct {
	GoType reflect.Type
	Err    error
}

Error is an error of the definition of a struct type.

type Field

type Field struct {
	Options
	// Index is the index of the field by reflect.Type.FieldByIndex: the fields of the embedded structs on the
	// way to it, and the field.
	Index []int
	Type  reflect.Type
	// ID is the place of the field in the breadth-first order of the fields.
	ID int
	// HasIsZero is whether the type of the field, or the pointer to it, has the IsZero method, which decides
	// whether the field is omitted by omitzero.
	HasIsZero bool
}

Field is a JSON object member of a struct.

type Fields

type Fields struct {
	// List is the fields in the depth-first order of their indexes, which is the order they are marshaled in.
	List []Field
	// Fallback is the embedded field of a map or of jsontext.Value which holds the members of no other field, or
	// nil.
	Fallback *Field
	// Err is the first error of the definition of the struct, or nil.
	Err *Error
	// FormatErr is the error of the first field with the `format` option, which encoding/json/v2 doesn't
	// support, or nil.
	FormatErr *Error
}

Fields are the JSON object members of a struct.

func Of

func Of(t reflect.Type) *Fields

Of returns the JSON object members of the struct type t.

type Options

type Options struct {
	Name       string // the JSON object name
	QuotedName string // Name as a JSON string
	HasName    bool   // whether the tag gives the name
	Casing     Casing
	Embed      bool
	OmitZero   bool
	OmitEmpty  bool
	String     bool
	Format     string
}

Options are the options of the `json` tag of a field.

Jump to

Keyboard shortcuts

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