json

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: Apache-2.0 Imports: 2 Imported by: 5

Documentation

Overview

Package json provides a configurable JSON encoding/decoding layer. It defaults to encoding/json but can be swapped for faster implementations like github.com/goccy/go-json or github.com/bytedance/sonic.

Usage:

import json "github.com/antflydb/antfly/pkg/libaf/json"

// Works like encoding/json
data, err := json.Marshal(v)
err = json.Unmarshal(data, &v)

To use a different JSON library:

import (
	json "github.com/antflydb/antfly/pkg/libaf/json"
	gojson "github.com/goccy/go-json"
)

func init() {
	json.SetConfig(json.Config{
		Marshal:         gojson.Marshal,
		MarshalIndent:   gojson.MarshalIndent,
		MarshalString:   gojson.MarshalString,
		Unmarshal:       gojson.Unmarshal,
		UnmarshalString: gojson.UnmarshalString,
		NewEncoder: func(w io.Writer) json.Encoder {
			return gojson.NewEncoder(w)
		},
		NewDecoder: func(r io.Reader) json.Decoder {
			return gojson.NewDecoder(r)
		},
	})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeIndented

func EncodeIndented(v any, prefix, indent string, opts ...EncodeOption) (result []byte, err error)

EncodeIndented returns the JSON encoding of v with indentation. It accepts optional EncodeOptions like SortMapKeys. Note: SortMapKeys is a no-op with encoding/json as it always sorts keys.

If the configured JSON library panics (e.g. goccy/go-json on nil union fields from oapi-codegen), it falls back to encoding/json. Note: opts and SortMapKeys are currently unused; they exist for future alternative JSON library support.

func Marshal

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

Marshal returns the JSON encoding of v.

func MarshalIndent

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

MarshalIndent is like Marshal but applies Indent to format the output.

func MarshalString

func MarshalString(v any) (string, error)

MarshalString returns the JSON encoding of v as a string.

func SetConfig

func SetConfig(c Config)

SetConfig sets the global JSON configuration. Call this before using any JSON functions to use a custom JSON library.

func Unmarshal

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

Unmarshal parses the JSON-encoded data and stores the result in v.

func UnmarshalString

func UnmarshalString(s string, v any) error

UnmarshalString parses the JSON-encoded string and stores the result in v.

Types

type Config

type Config struct {
	Marshal         func(v any) ([]byte, error)
	MarshalIndent   func(v any, prefix, indent string) ([]byte, error)
	MarshalString   func(v any) (string, error)
	Unmarshal       func(data []byte, v any) error
	UnmarshalString func(s string, v any) error
	NewEncoder      func(w io.Writer) Encoder
	NewDecoder      func(r io.Reader) Decoder
}

Config holds the JSON encoding/decoding functions.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration using encoding/json.

func GetConfig

func GetConfig() Config

GetConfig returns the current JSON configuration.

type Decoder

type Decoder interface {
	Decode(v any) error
}

Decoder is the interface for streaming JSON decoding. Both encoding/json and alternative libraries satisfy this interface.

func NewDecoder

func NewDecoder(r io.Reader) Decoder

NewDecoder returns a new Decoder that reads from r.

type EncodeOption

type EncodeOption func(*encodeOptions)

EncodeOption represents an option for JSON encoding.

var SortMapKeys EncodeOption = func(o *encodeOptions) {
	o.sortMapKeys = true
}

SortMapKeys is an option that sorts map keys in the output.

type Encoder

type Encoder interface {
	Encode(v any) error
}

Encoder is the interface for streaming JSON encoding. Both encoding/json and alternative libraries satisfy this interface.

func NewEncoder

func NewEncoder(w io.Writer) Encoder

NewEncoder returns a new Encoder that writes to w.

type Marshaler

type Marshaler = stdjson.Marshaler

Marshaler is the interface implemented by types that can marshal themselves into valid JSON.

type Number

type Number = stdjson.Number

Number represents a JSON number literal.

type RawMessage

type RawMessage = stdjson.RawMessage

RawMessage is a raw encoded JSON value. It implements Marshaler and Unmarshaler and can be used to delay JSON decoding.

type Unmarshaler

type Unmarshaler = stdjson.Unmarshaler

Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves.

Jump to

Keyboard shortcuts

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