json

package
v2.489.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package json provides the go-service JSON import path.

The package serves two related purposes:

  • it exposes Encoder, a thin adapter that satisfies the repository's generic encoding abstraction while preserving the default behavior of the standard library encoder and decoder
  • it re-exports common JSON helpers and types from the standard library so packages in this repository can depend on a single go-service JSON import path instead of importing encoding/json directly

All marshaling and unmarshaling semantics remain those of the standard library's encoding/json package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal added in v2.336.0

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

Marshal encodes v as JSON using the standard library implementation.

It exists so repository packages can use a single go-service JSON import path without changing standard library marshaling behavior.

func MarshalIndent added in v2.336.0

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

MarshalIndent encodes v as indented JSON using the standard library implementation.

It behaves exactly like `encoding/json.MarshalIndent`.

func Unmarshal added in v2.336.0

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

Unmarshal decodes JSON data into v using the standard library implementation.

In most cases v should be a pointer to the destination value. The decoding rules and error behavior are identical to `encoding/json.Unmarshal`.

func Valid added in v2.336.0

func Valid(data []byte) bool

Valid reports whether data is syntactically valid JSON.

It behaves exactly like `encoding/json.Valid`.

Types

type Encoder

type Encoder struct{}

Encoder implements JSON encoding and decoding.

It preserves the default behavior of the standard library `encoding/json` encoder and decoder and does not add repository-specific options.

func NewEncoder

func NewEncoder() *Encoder

NewEncoder constructs a JSON encoder.

NewEncoder returns an Encoder that satisfies `github.com/alexfalkowski/go-service/v2/encoding.Encoder` while delegating to the standard library's `encoding/json` implementation with its default settings.

func (*Encoder) Decode

func (e *Encoder) Decode(r io.Reader, v any) error

Decode reads JSON from r and decodes it into v.

In most cases v should be a pointer to the destination value, such as `*MyStruct` or `*map[string]any`. Decode is equivalent to calling `json.NewDecoder(r).Decode(v)`, then requiring the stream to contain no additional JSON values.

Duplicate JSON object keys keep the standard library's last-wins behavior.

func (*Encoder) Encode

func (e *Encoder) Encode(w io.Writer, v any) error

Encode writes v to w as JSON.

Encode is equivalent to calling `json.NewEncoder(w).Encode(v)`. As with the standard library, the encoded output is terminated with a trailing newline.

type Marshaler added in v2.336.0

type Marshaler = json.Marshaler

Marshaler aliases the standard library JSON marshaler interface.

Use this alias when a package wants to refer to the marshaling contract while keeping imports within the go-service JSON package.

type Number added in v2.336.0

type Number = json.Number

Number aliases the standard library JSON number type.

It is primarily useful with decoders configured to preserve numeric values as strings until the caller decides how to interpret them.

type RawMessage added in v2.336.0

type RawMessage = json.RawMessage

RawMessage aliases the standard library raw JSON message type.

It is useful when callers need to defer decoding or preserve the original encoded JSON payload.

type Unmarshaler added in v2.336.0

type Unmarshaler = json.Unmarshaler

Unmarshaler aliases the standard library JSON unmarshaler interface.

Use this alias when a package wants to refer to the unmarshaling contract while keeping imports within the go-service JSON package.

Jump to

Keyboard shortcuts

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