jsonx

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package jsonx is a mvm-aware replacement for the encoding/json functions that need to honour mvm-defined methods on struct types (MarshalJSON, UnmarshalJSON). It walks mvm *vm.Type metadata to traverse struct fields and dispatches mvm methods via Machine.CallFunc. Values whose mvm type is unknown are forwarded to the native encoding/json implementation.

Dispatch is wired through vm.RegisterArgProxy / RegisterArgProxyMethod: mvm Iface arguments to json.Marshal / Unmarshal / MarshalIndent and to (*Encoder).Encode / (*Decoder).Decode are wrapped as marshalProxy / unmarshalProxy pointers whose MarshalJSON / UnmarshalJSON methods re-enter the walker. Native encoding/json reflection sees the proxies as ordinary json.Marshaler / json.Unmarshaler implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder is the mvm-aware replacement for *encoding/json.Decoder. It wraps a native *json.Decoder; when mvm calls Decode, the argument arrives as a *unmarshalProxy that satisfies json.Unmarshaler, so native decoding drives UnmarshalJSON back into the walker.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new jsonx.Decoder that reads from r.

func (*Decoder) Buffered

func (d *Decoder) Buffered() io.Reader

Buffered forwards to (*json.Decoder).Buffered.

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

Decode reads the next JSON value from the stream into v.

func (*Decoder) DisallowUnknownFields

func (d *Decoder) DisallowUnknownFields()

DisallowUnknownFields forwards to (*json.Decoder).DisallowUnknownFields.

func (*Decoder) InputOffset

func (d *Decoder) InputOffset() int64

InputOffset forwards to (*json.Decoder).InputOffset.

func (*Decoder) More

func (d *Decoder) More() bool

More forwards to (*json.Decoder).More.

func (*Decoder) Token

func (d *Decoder) Token() (json.Token, error)

Token forwards to (*json.Decoder).Token.

func (*Decoder) UseNumber

func (d *Decoder) UseNumber()

UseNumber forwards to (*json.Decoder).UseNumber.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder is the mvm-aware replacement for *encoding/json.Encoder. Mvm resolves json.Encoder to this type via patchEncodingJSON, so user code that spells *json.Encoder actually gets *jsonx.Encoder. When mvm calls Encode with an Iface argument, vm.bridgeArgs wraps it as a *marshalProxy whose MarshalJSON re-enters the jsonx walker.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new jsonx.Encoder that writes to w, matching the signature of encoding/json.NewEncoder.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode serialises v as JSON and writes a newline-terminated line. Mvm callers receive v as a *marshalProxy (installed by vm.RegisterArgProxyMethod); native json.Marshal finds its MarshalJSON and recurses back into the walker.

func (*Encoder) SetEscapeHTML

func (e *Encoder) SetEscapeHTML(bool)

SetEscapeHTML matches the (*json.Encoder) method set for API compatibility. The flag is accepted but unused.

func (*Encoder) SetIndent

func (e *Encoder) SetIndent(prefix, indent string)

SetIndent mirrors (*json.Encoder).SetIndent.

Jump to

Keyboard shortcuts

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