enc

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2023 License: MIT Imports: 10 Imported by: 1

README

enc WIP

Replacement for encoding/json, providing an intermediate layer of abstraction between the encoded data and the typed data.

Interstitial

nodes := []enc.Node{
  enc.Nil{},
  enc.String("foo bar"),
  enc.Number(3.14),
  enc.Map{"null":enc.Nil{}},
  enc.List{enc.String("first")},
}

Rationale: in the built in encoding/json library, everything gets converted between []byte and specific types.

This means that when customization is needed, you end up implementing json.Unmarshaler or json.RawMessage.

This likely requires multiple scans to the data, and writing convoluted code or simply more complex.

Using an intermediate layer, which maps the primitive types in the JSON format, allows a single scan of the []byte data, and only high level computation on it.

Those primitive types can be then used when custom marshaling or unmarshaling.

enc.Pairs

This is a special type that can Marshal itself as a JSON object, but is implemented as a list of pairs, which then guarantee the order.

To keep the usability of this library high, we opted to avoid OrderedMap which are clumsy to use, and instead allow you to choose between the fast enc.Map, or the ordered enc.Pairs.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSON

func MarshalJSON(c ctx.C, from any) ([]byte, error)

func MustMarshalJSON

func MustMarshalJSON(c ctx.C, from any) []byte

func Unmarshal

func Unmarshal(c ctx.C, n Node, into any) error

func UnmarshalJSON

func UnmarshalJSON(c ctx.C, j []byte, into any) error

Types

type Bool

type Bool bool

func (Bool) GoString

func (this Bool) GoString() string

func (Bool) String

func (this Bool) String() string

type Codec

type Codec interface {
	Encode(c ctx.C, n Node) []byte
	Decode(c ctx.C, data []byte) (Node, error)
}

type Handler

type Handler struct {
	Factory map[reflect.Type]func(c ctx.C, n Node) (any, error)

	// called if a field is present in the NodeTree but there is no mapping on the object it's unmarshaled into
	UnhandledFields func(c ctx.C, path []any, n Node) error

	Debugf func(c ctx.C, f string, args ...any)
	// contains filtered or unexported fields
}

generic object that do the Unmarshal()/Conflate()

func (Handler) Append

func (this Handler) Append(p any) Handler

func (Handler) Marshal

func (this Handler) Marshal(c ctx.C, in any) (Node, error)

TODO(oha) no reason to have the Handler to Unmarshal()

func (Handler) Unmarshal

func (this Handler) Unmarshal(c ctx.C, n Node, into any) error

type JSON

type JSON struct {
}

func (JSON) Decode

func (this JSON) Decode(c ctx.C, data []byte) (Node, error)

func (JSON) Encode

func (this JSON) Encode(c ctx.C, n Node) []byte

type List

type List []Node

func (List) GoString

func (this List) GoString() string

func (List) String

func (this List) String() string

type Map

type Map map[string]Node

unordered map

func (Map) GoString

func (this Map) GoString() string

func (Map) MarshalJSON

func (this Map) MarshalJSON() ([]byte, error)

func (Map) String

func (this Map) String() string

type Marshaler

type Marshaler interface {
	MarshalNode(ctx.C) (Node, error)
}

obejcts which implements this can override how they are marshaled (Conflate)

type Nil

type Nil struct{}

func (Nil) GoString

func (this Nil) GoString() string

func (Nil) MarshalJSON

func (this Nil) MarshalJSON() ([]byte, error)

func (Nil) String

func (this Nil) String() string

type Node

type Node interface {
	String() string
	GoString() string
	// contains filtered or unexported methods
}

func Marshal

func Marshal(c ctx.C, in any) (Node, error)

transform an object into a enc.Node

func MustMarshal

func MustMarshal(c ctx.C, from any) Node

type Number

type Number float64

NOTE(oha) do we need to split into Integers and Floats?

func (Number) GoString

func (this Number) GoString() string

func (Number) String

func (this Number) String() string

type Pair

type Pair struct {
	Name string
	JSON string

	Value Node
}

type Pairs

type Pairs []Pair

ordered pairs, used mostly internally when Unmarshalling structs, to preserve the order of the fields can be used anywhere else where the order matters Note(oha): currently not used while decoding, to keep the decode stack simple and easy, might be changed in the future?

func (Pairs) Find

func (this Pairs) Find(name string) Node

func (Pairs) GoString

func (this Pairs) GoString() string

func (Pairs) MarshalJSON

func (this Pairs) MarshalJSON() ([]byte, error)

func (Pairs) String

func (this Pairs) String() string

type ReadWriteCloser

type ReadWriteCloser interface {
	Reader
	Writer
	io.Closer
}

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

type Reader

type Reader interface {
	Read(c ctx.C) (Node, error)
}

type ReaderCloser

type ReaderCloser interface {
	Reader
	io.Closer
}

type String

type String string

func (String) GoString

func (this String) GoString() string

func (String) String

func (this String) String() string

type Tag

type Tag struct {
	Name      string
	JSON      string
	OmitEmpty bool
	Skip      bool
}

type Unmarshaler

type Unmarshaler interface {
	UnmarshalNode(ctx.C, Node) error
}

objects which implements this can override how their data is unmarshaled (Expand)

type WriteCloser

type WriteCloser interface {
	io.Closer
	Writer
}

type Writer

type Writer interface {
	Write(c ctx.C, n Node) error
}

Jump to

Keyboard shortcuts

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