json

package
v2.681.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 5 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 using readable indented encoding and strict standard library decoding
  • 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

Marshal and Valid preserve the standard library's encoding/json semantics. Encoder and Unmarshal use the standard decoder with unknown fields and trailing values rejected. Duplicate object keys keep the standard library's last-wins behavior.

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 readable indented JSON.

It exists so repository packages can use a single go-service JSON import path.

func Unmarshal added in v2.336.0

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

Unmarshal decodes one JSON value from data into v.

It uses Decode, so it rejects additional JSON values after the first payload.

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 writes readable indented JSON and uses the standard library encoding/json decoder with unknown fields and trailing values rejected.

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 readable indented encoding and strict decoding.

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)` with unknown fields rejected, 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).SetIndent("", " ")` before encoding 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