json

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: Apache-2.0 Imports: 2 Imported by: 65

README

Go Reference Go Report Go Version License json Version Discord

json

json provides configurable interfaces for JSON encoding/decoding and a nullable type which can be used to represent JSON nullable & optional values.

Getting Started

Installing
go get github.com/disgoorg/json

Documentation

Documentation is wip and can be found under

  • Go Reference

Troubleshooting

For help feel free to open an issue or reach out on Discord

Contributing

Contributions are welcomed but for bigger changes we recommend first reaching out via Discord or create an issue to discuss your problems, intentions and ideas.

License

Distributed under the License. See LICENSE for more information.

Documentation

Overview

Package json provides configurable interfaces for JSON encoding and decoding.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Marshal marshals the given value into a JSON string.
	Marshal = json.Marshal

	// Unmarshal unmarshals the given JSON string into v.
	Unmarshal = json.Unmarshal

	// Merge merges two JSON objects into one.
	Merge = SimpleMerge

	// MarshalIndent marshals the given value into a JSON string with indentation.
	MarshalIndent = json.MarshalIndent

	// Indent is the indentation used in MarshalIndent.
	Indent = json.Indent

	// NewEncoder returns a new JSON encoder that writes to w.
	NewEncoder = json.NewEncoder

	// NewDecoder returns a new JSON decoder that reads from r.
	NewDecoder = json.NewDecoder
)
View Source
var NullBytes = []byte("null")

NullBytes is a byte slice containing the JSON null literal.

Functions

func Ptr

func Ptr[T any](t T) *T

Ptr returns a pointer of t.

func SimpleMerge added in v1.1.0

func SimpleMerge(data1 []byte, data2 []byte) ([]byte, error)

SimpleMerge merges two JSON objects into one. If a key exists in both objects, the value from the second object is used.

Types

type Marshaler

type Marshaler = json.Marshaler

Marshaler is the interface implemented by types that can marshal JSON.

type Nullable

type Nullable[T any] struct {
	// contains filtered or unexported fields
}

Nullable represents a nullable value. It gets serialized as either null or the value.

func NewNullable

func NewNullable[T any](t T) Nullable[T]

NewNullable returns a new Nullable with t as the value.

func NewNullablePtr

func NewNullablePtr[T any](t T) *Nullable[T]

NewNullablePtr returns a pointer to a new Nullable with t as the value.

func Null

func Null[T any]() Nullable[T]

Null returns a null Nullable.

func NullPtr

func NullPtr[T any]() *Nullable[T]

NullPtr returns a pointer to a null Nullable.

func (Nullable[T]) IsNull

func (n Nullable[T]) IsNull() bool

IsNull returns true if the Nullable is null.

func (Nullable[T]) MarshalJSON

func (n Nullable[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface.

func (*Nullable[T]) UnmarshalJSON

func (n *Nullable[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the Unmarshaler interface.

func (Nullable[T]) Value

func (n Nullable[T]) Value() T

Value returns the value of the Nullable. If the Nullable is null, it returns the zero value of T.

type RawMessage

type RawMessage = json.RawMessage

RawMessage is a raw encoded JSON value.

type Unmarshaler

type Unmarshaler = json.Unmarshaler

Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.

Jump to

Keyboard shortcuts

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