zstdzgo.at/zstd/zjson Index | Files

package zjson

import "zgo.at/zstd/zjson"

Package zjson provides functions for working with JSON.

Index

Functions

func Get

func Get[T Types](j []byte, path string) (T, error)

Get a value from JSON bytes.

Format of path:

foo
foo.nested

func Indent

func Indent(data []byte, v any, prefix, indent string) ([]byte, error)

Indent a json string by unmarshalling it and marshalling it with MarshalIndent.

The data will be unmarshalled in to v, which must be a pointer. Example:

Indent(`{"a": "b"}`, &map[string]string{}, "", "  ")

func IndentString

func IndentString(data []byte, v any, prefix, indent string) (string, error)

IndentString is like Indent, but returns a string.

func MustGet

func MustGet[T Types](j []byte, path string) T

MustGet is like Get, but will panic on errors.

func MustIndent

func MustIndent(data []byte, v any, prefix, indent string) []byte

MustIndent behaves like Indent but will panic on errors.

func MustIndentString

func MustIndentString(data []byte, v any, prefix, indent string) string

MustIndentString is like MustIndent, but returns a string.

func MustMarshal

func MustMarshal(v any) []byte

MustMarshal behaves like json.Marshal but will panic on errors.

func MustMarshalIndent

func MustMarshalIndent(v any, prefix, indent string) []byte

MustMarshalIndent behaves like json.MarshalIndent but will panic on errors.

func MustMarshalIndentString

func MustMarshalIndentString(v any, prefix, indent string) string

MustMarshalIndentString is like MustMarshalIndent, but returns a string.

func MustMarshalString

func MustMarshalString(v any) string

MustMarshalString is like MustMarshal, but returns a string.

func MustUnmarshal

func MustUnmarshal(data []byte, v any)

MustUnmarshal behaves like json.Unmarshal but will panic on errors.

func MustUnmarshalTo

func MustUnmarshalTo(data []byte, target reflect.Type) any

MustUnmarshalTo behaves like UnmarshalTo but will panic on errors.

func UnmarshalTo

func UnmarshalTo(data []byte, target reflect.Type) (any, error)

UnmarshalTo unmarshals the JSON in data to a new instance of target.

Target can be any type that json.Unmarshal can unmarshal to, and doesn't need to be a pointer. The returned value is always a pointer.

Types

type Int

type Int int64

Int for APIs that return numbers as strings.

func (Int) MarshalJSON

func (i Int) MarshalJSON() ([]byte, error)

Marshal in to JSON.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(v []byte) error

Unmarshal a string timestamp as an int.

type Timestamp

type Timestamp struct{ time.Time }

Timestamp for APIs that return dates as a numeric Unix timestamp.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

Marshal in to JSON.

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(v []byte) error

Unmarshal a Unix timestamp as a date.

type Types

type Types interface {
	~float64 | ~string | ~[]any | ~map[string]any
}

Types that can occur in JSON documents.

Source Files

zjson.go

Version
v0.0.0-20240930202209-a63c3335042a (latest)
Published
Sep 30, 2024
Platform
linux/amd64
Imports
8 packages
Last checked
2 days ago

Tools for package owners.