json

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: Apache-2.0 Imports: 16 Imported by: 110

Documentation

Overview

Package json implements the JSON-Cadence specification: https://github.com/onflow/flow/blob/master/docs/json-cadence-spec.md

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(gauge common.MemoryGauge, b []byte, options ...Option) (cadence.Value, error)

Decode returns a Cadence value decoded from its JSON-encoded representation.

This function returns an error if the bytes represent JSON that is malformed or does not conform to the JSON Cadence specification.

func Encode

func Encode(value cadence.Value) ([]byte, error)

Encode returns the JSON-encoded representation of the given value.

This function returns an error if the Cadence value cannot be represented as JSON.

func MustEncode added in v0.2.0

func MustEncode(value cadence.Value) []byte

MustEncode returns the JSON-encoded representation of the given value, or panics if the value cannot be represented as JSON.

func Prepare added in v0.12.0

func Prepare(v cadence.Value) jsonValue

Prepare traverses the object graph of the provided value and constructs a struct representation that can be marshalled to JSON.

func PrepareType added in v1.0.0

func PrepareType(typ cadence.Type, results TypePreparationResults) jsonValue

Types

type Decoder

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

A Decoder decodes JSON-encoded representations of Cadence values.

func NewDecoder

func NewDecoder(gauge common.MemoryGauge, r io.Reader) *Decoder

NewDecoder initializes a Decoder that will decode JSON-encoded bytes from the given io.Reader.

func (*Decoder) Decode

func (d *Decoder) Decode() (value cadence.Value, err error)

Decode reads JSON-encoded bytes from the io.Reader and decodes them to a Cadence value.

This function returns an error if the bytes represent JSON that is malformed or does not conform to the JSON Cadence specification.

func (*Decoder) DecodeJSON added in v1.0.0

func (d *Decoder) DecodeJSON(v any) cadence.Value

type Encoder

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

An Encoder converts Cadence values into JSON-encoded bytes.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder initializes an Encoder that will write JSON-encoded bytes to the given io.Writer.

func (*Encoder) Encode

func (e *Encoder) Encode(value cadence.Value) (err error)

Encode writes the JSON-encoded representation of the given value to this encoder's io.Writer.

This function returns an error if the given value's type is not supported by this encoder.

type Option added in v0.24.4

type Option func(*Decoder)

func WithAllowUnstructuredStaticTypes added in v0.24.4

func WithAllowUnstructuredStaticTypes(allow bool) Option

WithAllowUnstructuredStaticTypes returns a new Decoder Option which enables or disables if the decoding of a static type as a type ID (cadence.TypeID) is allowed

func WithBackwardsCompatibility added in v1.0.0

func WithBackwardsCompatibility() Option

WithBackwardsCompatibility returns a new Decoder Option which enables backwards compatibility mode, where the decoding of old versions of the JSON encoding is allowed

type TypePreparationResults added in v1.0.0

type TypePreparationResults map[cadence.Type]struct{}

Jump to

Keyboard shortcuts

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