Skip to main content

Module encoders

Module encoders 

Source
Expand description

Serialization and deserialization traits and implementations.

This module provides the core abstraction layer for encoding Rust types into bytes and decoding bytes back into Rust types. It is the foundation of the library’s “any-format” design, allowing seamless format selection at runtime.

§Overview

The encoders module defines two primary traits:

  • Encoder - Converts Rust types into byte sequences
  • Decoder - Converts byte sequences back into Rust types

These traits are object-safe and thread-safe, making them suitable for dynamic dispatch through trait objects. This enables applications to support multiple serialization formats without recompiling, or even switch formats at runtime based on configuration or message metadata.

§Built-in Formats

The library includes ready-to-use implementations for:

  • JSON (feature json) - Human-readable, widely compatible, available via [json] module
  • MessagePack (feature msgpack) - Compact binary format, faster than JSON, available via [msgpack] module

§Custom Formats

You are not limited to built-in formats. Implement Encoder and Decoder for any serialization format you need:

  • Other binary formats: CBOR, Protocol Buffers, Avro, Parquet
  • Custom formats: domain-specific binary protocols, compressed formats
  • Specialized formats: for specific use cases or performance requirements

§Feature Flags

  • json - Enables JSON encoding/decoding support
  • msgpack - Enables MessagePack encoding/decoding support

§Examples

Using the JSON encoder/decoder:

use serde::{Serialize, Deserialize};
use object_transfer::encoders::{Encoder, Decoder};
#[cfg(feature = "json")]
use object_transfer::encoders::{JSONEncoder, JSONDecoder};

#[derive(Serialize, Deserialize)]
struct Event {
    id: u32,
    name: String,
}

let encoder = JSONEncoder::new();
let decoder = JSONDecoder::new();

let event = Event { id: 1, name: "test".to_string() };
let bytes = encoder.encode(&event)?;
let decoded: Event = decoder.decode(bytes)?;

Traits§

Decoder
A trait for decoding byte sequences back into items.
Encoder
A trait for encoding items into byte sequences.