Skip to content

Serialization

When using System.Text.Json, byte[] values are serialized as base64-encoded strings. Since IReadableByteBuffer represents binary data, it is serialized using the same base64 representation by default.

However, it is possible to change this behaviour and serialize the buffer as a hex string instead. This can be useful for consumers that expect such format, or for debugging purposes, since it is often more readable when inspecting binary protocol data.

To do this, simply annotate the property with the ByteBufferJsonConverter attribute and set the HexString format.

public class MyObject
{
    public string? Id { get; set; }

    [ByteBufferJsonConverter( Format = ByteBufferSerializerFormat.HexString )]
    public IReadableByteBuffer? Buffer { get; set; }
}

Note

This attribute is only available when using System.Text.Json serializer. If you are using Newtonsoft.Json, you will need to implement a custom converter to achieve the same result.