jsonenc

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package jsonenc encodes values as encoding/json does, byte for byte, except that a nil slice is written as [] instead of null: Tern sends [] for pages, typed endpoints and live components, so their TypeScript types are T[] rather than T[] | null.

It's faster than encoding/json followed by a copy that fills in nil slices: each type's encoder is built once, and nothing is copied. Values that encode themselves (json.Marshaler, encoding.TextMarshaler) and the rare struct tag options it doesn't implement go through encoding/json.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(b []byte, v any) ([]byte, error)

Append appends the JSON encoding of v to b.

func EncodesItself added in v1.8.0

func EncodesItself(t reflect.Type) bool

EncodesItself reports whether values of t are written by their own MarshalJSON or MarshalText, or need encoding/json for another reason: what this package hands to encoding/json.

func IsEmpty added in v1.8.0

func IsEmpty(v reflect.Value) bool

IsEmpty and IsZero are the omitempty and omitzero tests.

func IsZero added in v1.8.0

func IsZero(v reflect.Value) bool

IsZero is the omitzero test.

func MapKey added in v1.8.0

func MapKey(k reflect.Value) (string, bool)

MapKey is a map key as encoding/json writes it, for the map kinds this package encodes itself: string and integer keys.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal returns the JSON encoding of v.

Types

type Field added in v1.8.0

type Field struct {
	Name string
	// Index is the path to the field through embedded structs.
	Index     []int
	Type      reflect.Type
	OmitEmpty bool
	OmitZero  bool
}

Field is a struct field as encoding/json writes it, for other encoders that follow its rules.

func Fields added in v1.8.0

func Fields(t reflect.Type) (fields []Field, ok bool)

Fields returns the fields encoding/json writes for struct type t, in order. ok is false for structs it leaves to encoding/json (the ",string" option).

type Verbatim

type Verbatim []byte

Verbatim is JSON this package wrote, written again as it is: no checking, compacting or escaping. Only for its own output, which is already compact and safe to inline in HTML.

func (Verbatim) MarshalJSON

func (v Verbatim) MarshalJSON() ([]byte, error)

MarshalJSON is for encoding/json, which checks and compacts it.

func (*Verbatim) UnmarshalJSON

func (v *Verbatim) UnmarshalJSON(b []byte) error

UnmarshalJSON keeps the raw JSON, as json.RawMessage does.

Jump to

Keyboard shortcuts

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