Path: redb-route/redb.Route/Processors/ClaimCheckSerializer.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Processors/ClaimCheckSerializer.cs
----
using System;
using System.Text;
using System.Text.Json;
namespace redb.Route.Processors;
///
/// Default serialization helper for the Claim Check pattern.
/// Handles byte[], string, and arbitrary objects (via JSON).
///
internal static class ClaimCheckSerializer
{
///
/// Serializes a message body to a byte array.
///
/// The body to serialize. May be null, byte[], string, or any serializable object.
/// Binary representation of the body.
public static byte[] Serialize(object? body)
{
if (body is null) return [];
if (body is byte[] bytes) return bytes;
if (body is string str) return Encoding.UTF8.GetBytes(str);
if (body is ReadOnlyMemory rom) return rom.ToArray();
if (body is Memory mem) return mem.ToArray();
return JsonSerializer.SerializeToUtf8Bytes(body, body.GetType());
}
///
/// Deserializes a byte array back to a message body.
/// Returns the data as a string (UTF-8) by default; callers can use ConvertBody afterwards.
///
/// The binary data to deserialize.
/// Original CLR type name, or null.
/// The deserialized body.
public static object? Deserialize(byte[] data, string? originalBodyType)
{
if (data.Length == 0) return null;
// If original was byte[], return byte[]
if (originalBodyType == typeof(byte[]).FullName)
return data;
// Default: return as UTF-8 string
return Encoding.UTF8.GetString(data);
}
}