Encoding & Decoding

Altruist provides a flexible Codec system that allows developers to define how data is serialized and deserialized when communicating between the client and server.

By default, JsonCodec is used for compatibility, but for performance-critical scenarios, you can switch to MessagePackCodec, which offers faster encoding and decoding.

Available Codecs

CodecDescription
JsonCodecDefault codec using JSON serialization for compatibility.
MessagePackCodecA faster alternative using MessagePack for efficient binary serialization.

Note:

Tip: MessagePack is significantly faster than JSON and reduces payload size, making it ideal for high-performance applications.

Enabling a Custom Codec

To set a specific codec, it must be defined before selecting the application type (.WebApp()).

Example: Using MessagePackCodec

AltruistBuilder.Create(args)
    .Codec<MessagePackCodec>() // Set MessagePack as the codec
    .WebApp() // Define the application type
    .StartServer();

This configuration tells Altruist to use MessagePack instead of JSON for encoding and decoding data.

Why Use a Custom Codec?

Switching to MessagePackCodec can greatly improve network performance by:

  • Reducing payload size: Binary formats are more compact than JSON.
  • Faster serialization/deserialization: MessagePack is optimized for speed.
  • Lowering network bandwidth usage: Smaller data transfers lead to better efficiency.

However, JSON remains the most compatible choice, especially when debugging or interacting with web-based clients.

Summary

FeatureJsonCodecMessagePackCodec
Compatibility✅ High⚠️ Requires MessagePack support
Speed🟡 Moderate🟢 Fast
Payload Size🟡 Large🟢 Small
Ideal Use CaseGeneral use, debugging, web clientsPerformance-critical applications

Creating a Custom Codec

If the default JsonCodec and MessagePackCodec do not fit your needs, Altruist allows you to define your own custom codec by implementing the ICodec interface.

ICodec Interface

A codec in Altruist consists of two main components:

  • IEncoder: Responsible for encoding (serializing) objects.
  • IDecoder: Responsible for decoding (deserializing) objects.

The ICodec interface is structured as follows:

public interface ICodec
{
    IEncoder Encoder { get; }
    IDecoder Decoder { get; }
}

Implementing a Custom Codec

To create a custom codec, you need to implement ICodec and provide custom IEncoder and IDecoder implementations.

public class CustomCodec : ICodec
{
    public IEncoder Encoder { get; } = new CustomEncoder();
    public IDecoder Decoder { get; } = new CustomDecoder();
}

public class CustomEncoder : IEncoder
{
    public byte[] Encode<T>(T obj)
    {
        // Custom serialization logic here
    }

    public byte[] Encode(object data, Type type)
    {
        // Custom serialization logic here
    }
}

public class CustomDecoder : IDecoder
{
    public T Decode<T>(byte[] data)
    {
        // Custom deserialization logic here
    }

    public object Decode(byte[] data, Type type)
    {
        // Custom deserialization logic here
    }

    public T Decode<T>(byte[] data, Type type)
    {
       return Decode(data, type) as T;
    }
}

Using the Custom Codec Once you've created a custom codec, you can enable it in your server configuration like this:

AltruistBuilder.Create(args)
    .Codec<CustomCodec>() // Register the custom codec
    .WebApp()
    .StartServer();

Note:

By defining a custom codec, you gain full control over how data is serialized and deserialized, making it possible to integrate specialized encoding formats or optimize performance further.