jsonname

package module
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: Apache-2.0 Imports: 3 Imported by: 4

Documentation

Overview

Package jsonname is a provider of json property names from go properties.

Index

Constants

This section is empty.

Variables

View Source
var DefaultJSONNameProvider = NewNameProvider()

DefaultJSONNameProvider is the default cache for types.

Functions

This section is empty.

Types

type GoNameProvider added in v0.26.0

type GoNameProvider struct {
	// contains filtered or unexported fields
}

GoNameProvider resolves json property names to go struct field names following the same rules as the standard library's encoding/json package.

Contrary to NameProvider, it considers exported fields without a json tag, and promotes fields from anonymous embedded struct types.

Rules (aligned with encoding/json):

  • unexported fields are ignored;
  • a field tagged `json:"-"` is ignored;
  • a field tagged `json:"-,"` is kept under the json name "-" (stdlib quirk);
  • a field tagged `json:""` or with no json tag at all keeps its Go name as json name;
  • anonymous struct fields without an explicit json tag have their fields promoted into the parent, following breadth-first depth rules: a shallower field wins over a deeper one; at equal depth, a conflict discards all conflicting fields unless exactly one has an explicit json tag.

This type is safe for concurrent use.

func NewGoNameProvider added in v0.26.0

func NewGoNameProvider() *GoNameProvider

NewGoNameProvider creates a new GoNameProvider.

func (*GoNameProvider) GetGoName added in v0.26.0

func (n *GoNameProvider) GetGoName(subject any, name string) (string, bool)

GetGoName gets the go name for a json property name.

func (*GoNameProvider) GetGoNameForType added in v0.26.0

func (n *GoNameProvider) GetGoNameForType(tpe reflect.Type, name string) (string, bool)

GetGoNameForType gets the go name for a given type for a json property name.

func (*GoNameProvider) GetJSONName added in v0.26.0

func (n *GoNameProvider) GetJSONName(subject any, name string) (string, bool)

GetJSONName gets the json name for a go property name.

func (*GoNameProvider) GetJSONNameForType added in v0.26.0

func (n *GoNameProvider) GetJSONNameForType(tpe reflect.Type, name string) (string, bool)

GetJSONNameForType gets the json name for a go property name on a given type.

func (*GoNameProvider) GetJSONNames added in v0.26.0

func (n *GoNameProvider) GetJSONNames(subject any) []string

GetJSONNames gets all the json property names for a type.

type NameProvider

type NameProvider struct {
	// contains filtered or unexported fields
}

NameProvider represents an object capable of translating from go property names to json property names.

This type is thread-safe.

See github.com/go-openapi/jsonpointer.Pointer for an example.

func NewNameProvider

func NewNameProvider() *NameProvider

NewNameProvider creates a new name provider

func (*NameProvider) GetGoName

func (n *NameProvider) GetGoName(subject any, name string) (string, bool)

GetGoName gets the go name for a json property name

func (*NameProvider) GetGoNameForType

func (n *NameProvider) GetGoNameForType(tpe reflect.Type, name string) (string, bool)

GetGoNameForType gets the go name for a given type for a json property name

func (*NameProvider) GetJSONName

func (n *NameProvider) GetJSONName(subject any, name string) (string, bool)

GetJSONName gets the json name for a go property name

func (*NameProvider) GetJSONNameForType

func (n *NameProvider) GetJSONNameForType(tpe reflect.Type, name string) (string, bool)

GetJSONNameForType gets the json name for a go property name on a given type

func (*NameProvider) GetJSONNames

func (n *NameProvider) GetJSONNames(subject any) []string

GetJSONNames gets all the json property names for a type

Jump to

Keyboard shortcuts

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