respjson

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: Apache-2.0 Imports: 0 Imported by: 2

Documentation

Index

Constants

View Source
const Null string = "null"
View Source
const Omitted string = ""

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

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

A Field provides metadata to indicate the presence of a value.

Use Field.Valid to check if an optional value was null or omitted.

A Field will always occur in the following structure, where it mirrors the original field in it's parent struct:

type ExampleObject struct {
	Foo bool	`json:"foo"`
	Bar int		`json:"bar"`
	// ...

	// JSON provides metadata about the object.
	JSON struct {
		Foo Field
		Bar Field
		// ...
	} `json:"-"`
}

To differentiate a "nullish" value from the zero value, use the Field.Valid method.

if !example.JSON.Foo.Valid() {
	println("Foo is null or omitted")
}

if example.Foo {
	println("Foo is true")
} else {
	println("Foo is false")
}

To differentiate if a field was omitted or the JSON value "null", use the Field.Raw method.

if example.JSON.Foo.Raw() == "null" {
	println("Foo is null")
}

if example.JSON.Foo.Raw() == "" {
	println("Foo was omitted")
}

Otherwise, if the field was invalid and couldn't be marshalled successfully, Field.Valid will be false and Field.Raw will not be empty.

func NewField

func NewField(raw string) Field

func NewInvalidField

func NewInvalidField(raw string) Field

func (Field) Raw

func (j Field) Raw() string

Returns the raw JSON value of the field.

func (Field) Valid

func (j Field) Valid() bool

Valid returns true if the parent field was set. Valid returns false if the value doesn't exist, is JSON null, or is an unexpected type.

Jump to

Keyboard shortcuts

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