Path: redb-route/redb.Route/Serialization/MarshalProcessor.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/Serialization/MarshalProcessor.cs ---- using redb.Route.Abstractions; using redb.Route.Processors; namespace redb.Route.Serialization; /// /// Processor that marshals the exchange body to/from bytes using an . /// Use in a pipeline to serialize before sending or deserialize after receiving. /// public sealed class MarshalProcessor : IProcessor { private readonly IMessageSerializer _serializer; /// Creates a marshal (serialize) processor. /// Serializer to use. public MarshalProcessor(IMessageSerializer serializer) { _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); } /// public Task Process(IExchange exchange, CancellationToken ct = default) { // A message format sees the whole message (a body may be empty when the attachments are the payload). if (_serializer is IMessageDataFormat messageFormat) return messageFormat.MarshalAsync(exchange, ct).AsTask(); var body = exchange.In.Body; if (body is null) return Task.CompletedTask; // For an object-model format (JSON, XML, Avro, ...) a byte[] body is an already-marshalled result // and passes through; for a byte wrapper (GZip, Zip, Base64) the bytes are the payload itself. if (body is byte[] && !_serializer.WrapsBytes) return Task.CompletedTask; var bytes = SerializeBody(body); exchange.In.Body = bytes; exchange.In.ContentType = _serializer.ContentType; exchange.In.Headers["Content-Type"] = _serializer.ContentType; return Task.CompletedTask; } private byte[] SerializeBody(object body) { // Use reflection to call Serialize with the actual runtime type var method = typeof(IMessageSerializer) .GetMethod(nameof(IMessageSerializer.Serialize))! .MakeGenericMethod(body.GetType()); return (byte[])method.Invoke(_serializer, [body])!; } } /// /// Processor that unmarshals the exchange body from bytes to a typed object /// using an . /// public sealed class UnmarshalProcessor : IProcessor { private readonly IMessageSerializer _serializer; private readonly Type _targetType; /// Creates an unmarshal (deserialize) processor. /// Serializer to use. /// Type to deserialize to. public UnmarshalProcessor(IMessageSerializer serializer, Type targetType) { _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); _targetType = targetType ?? throw new ArgumentNullException(nameof(targetType)); } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { // A message format reads the body as it came, with the content type parameters. if (_serializer is IMessageDataFormat messageFormat) { await messageFormat.UnmarshalAsync(exchange, _targetType, ct).ConfigureAwait(false); return; } // Text formats (CSV, YAML, JSON) usually arrive as a string; a Stream is common after an // HTTP or file consumer. All three are the same bytes to a serializer. var bytes = exchange.In.Body switch { byte[] b => b, string s => System.Text.Encoding.UTF8.GetBytes(s), Stream stream => await ReadAllAsync(stream, ct).ConfigureAwait(false), _ => null, }; if (bytes is null) return; exchange.In.Body = _serializer.Deserialize(bytes, _targetType); } private static async Task ReadAllAsync(Stream stream, CancellationToken ct) { // The buffer shortcut only when the stream is at its start; a positioned stream (an earlier step // consumed framing) is read from its position like any other, never from byte zero. if (stream is MemoryStream ms && ms.Position == 0 && ms.TryGetBuffer(out var segment) && segment.Offset == 0 && segment.Count == ms.Length) return segment.Array!.Length == segment.Count ? segment.Array : segment.ToArray(); using var buffer = new MemoryStream(); await stream.CopyToAsync(buffer, ct).ConfigureAwait(false); return buffer.ToArray(); } }