jsonrs

package
v0.59.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: AGPL-3.0 Imports: 5 Imported by: 82

Documentation

Index

Constants

View Source
const (
	// StdLib is the JSON implementation of package/json.
	StdLib = "std"
	// SonnetLib is the JSON implementation of github.com/rudderlabs/sonnet.
	SonnetLib = "sonnet"
	// JsoniterLib is the JSON implementation of github.com/json-iterator/go.
	JsoniterLib = "jsoniter"
	// DefaultLib is the default JSON implementation.
	DefaultLib = SonnetLib
)

Variables

View Source
var Default = New(config.Default)

Functions

func Marshal

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

Marshal returns the JSON encoding of v. Uses the default JSON implementation.

func MarshalIndent

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

MarshalIndent returns the JSON encoding of v with indentation. Uses the default JSON implementation.

func MarshalToString

func MarshalToString(v any) (string, error)

MarshalToString returns the JSON encoding of v as a string. Uses the default JSON implementation.

func Reset

func Reset()

Reset resets the default JSON implementation based on the default configuration.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. Uses the default JSON implementation.

func Valid

func Valid(data []byte) bool

Valid returns true if the data is valid JSON. Uses the default JSON implementation.

Types

type Decoder

type Decoder interface {
	Buffered() io.Reader
	Decode(v any) error
	DisallowUnknownFields()
	More() bool
	UseNumber()
}

Decoder is the interface that wraps the basic JSON decoder operations.

func NewDecoder

func NewDecoder(r io.Reader) Decoder

NewDecoder returns a new json decoder that reads from r. Uses the default JSON implementation.

type Encoder

type Encoder interface {
	Encode(v any) error
	SetEscapeHTML(on bool)
	SetIndent(prefix, indent string)
}

Encoder is the interface that wraps the basic JSON encoder operations.

func NewEncoder

func NewEncoder(w io.Writer) Encoder

NewEncoder returns a new json encoder that writes to w. Uses the default JSON implementation.

type JSON

type JSON interface {
	Marshaller
	Unmarshaller
	Validator
}

JSON is the interface that wraps the basic JSON operations.

func New

func New(conf *config.Config) JSON

New returns a new JSON implementation based on the configuration.

func NewWithLibrary

func NewWithLibrary(library string) JSON

NewWithLibrary returns a new JSON implementation based on the library.

type Marshaller

type Marshaller interface {
	// Marshal returns the JSON encoding of v.
	Marshal(v any) ([]byte, error)
	// MarshalToString returns the JSON encoding of v as a string.
	MarshalToString(v any) (string, error)
	// MarshalIndent returns the JSON encoding of v with indentation.
	MarshalIndent(v any, prefix, indent string) ([]byte, error)
	// NewEncoder returns a new json encoder that writes to an io.Writer
	NewEncoder(w io.Writer) Encoder
}

type Unmarshaller

type Unmarshaller interface {
	// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
	Unmarshal(data []byte, v any) error
	// NewDecoder returns a new json decoder that reads from an io.Reader.
	NewDecoder(r io.Reader) Decoder
}

type Validator

type Validator interface {
	Valid(data []byte) bool
}

Jump to

Keyboard shortcuts

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