serde

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: BSD-3-Clause Imports: 1 Imported by: 15

Documentation

Overview

Package serde defines the serialization and deserialization mechanisms.

The serialization works through the implementation of the Message interface that can either support a single format, or a dynamic registration of a format engine for each format.

The deserialization works in a similar fashion but through the Factory interface.

See dela/serde/registry for more advanced control of the formats.

Documentation Last Review: 07.10.2020

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	ContextEngine
	// contains filtered or unexported fields
}

Context is the context passed to the serialization/deserialization requests.

func NewContext

func NewContext(engine ContextEngine) Context

NewContext returns a new empty context.

func WithFactory

func WithFactory(ctx Context, key interface{}, f Factory) Context

WithFactory adds a factory to the context. The factory will then be availble with the key when deserializing.

func (Context) GetFactory

func (ctx Context) GetFactory(key interface{}) Factory

GetFactory returns the factory associated to the key or nil.

type ContextEngine

type ContextEngine interface {
	// GetFormat returns the name of the format for this context.
	GetFormat() Format

	// Marshal returns the bytes of the message according to the format of the
	// context.
	Marshal(message interface{}) ([]byte, error)

	// Unmarshal populates the message with the data according to the format of
	// the context.
	Unmarshal(data []byte, message interface{}) error
}

ContextEngine is the interface to implement to create a context.

type Factory

type Factory interface {
	// Deserialize deserializes the message instantiated from the data.
	Deserialize(ctx Context, data []byte) (Message, error)
}

Factory is the interface that a message factory must implement.

type Fingerprinter

type Fingerprinter interface {
	// Fingerprint writes a deterministic binary representation of the object
	// into the writer.
	Fingerprint(writer io.Writer) error
}

Fingerprinter is an interface to fingerprint an object.

type Format

type Format string

Format is the identifier type of a format implementation.

const (
	// FormatJSON is the identifier for JSON formats.
	FormatJSON Format = "JSON"

	// FormatXML is the identifier for XML formats.
	FormatXML Format = "XML"
)

type FormatEngine

type FormatEngine interface {
	// Encode marshals the message according to the format definition.
	Encode(ctx Context, message Message) ([]byte, error)

	// Decode unmarshal a message according to the format definition.
	Decode(ctx Context, data []byte) (Message, error)
}

FormatEngine is the interface that a format implementation must implement.

type Message

type Message interface {
	// Serialize serializes the object by complying to the context format.
	Serialize(ctx Context) ([]byte, error)
}

Message is the interface that a message must implement.

Directories

Path Synopsis
Package json implements the context engine for a the JSON format.
Package json implements the context engine for a the JSON format.
Package registry defines the format registry mechanism.
Package registry defines the format registry mechanism.
Package xml implements the context engine for the XML encoding.
Package xml implements the context engine for the XML encoding.

Jump to

Keyboard shortcuts

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