package serde
import "go.dedis.ch/dela/serde"
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 ¶
- type Context
- func NewContext(engine ContextEngine) Context
- func WithFactory(ctx Context, key interface{}, f Factory) Context
- func (ctx Context) GetFactory(key interface{}) Factory
- type ContextEngine
- type Factory
- type Fingerprinter
- type Format
- type FormatEngine
- type Message
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 ¶
WithFactory adds a factory to the context. The factory will then be availble with the key when deserializing.
func (Context) GetFactory ¶
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.
Source Files ¶
context.go serde.go
Directories ¶
Path | Synopsis |
---|---|
serde/json | Package json implements the context engine for a the JSON format. |
serde/registry | Package registry defines the format registry mechanism. |
serde/xml | Package xml implements the context engine for the XML encoding. |
- Version
- v0.1.0 (latest)
- Published
- Apr 10, 2024
- Platform
- linux/amd64
- Imports
- 1 packages
- Last checked
- 1 month ago –
Tools for package owners.