Path: redb-route/redb.Route/Definitions/SerializationDefinition.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/Definitions/SerializationDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Serialization;
namespace redb.Route.Definitions;
///
/// Resolves the serializer of a Marshal / Unmarshal node from whichever form the DSL
/// used: an instance (options per node), a content type (looked up in
/// at route build — an unregistered format fails Start()), or a serializer type (instantiated
/// with its parameterless constructor).
///
internal static class DataFormatResolution
{
public static IMessageSerializer Resolve(IRouteContext context, Type? serializerType, IMessageSerializer? serializer, string? contentType)
{
if (serializer is not null)
return serializer;
if (contentType is not null)
{
var registry = context.GetService()
?? throw new InvalidOperationException("IDataFormatRegistry is not available on the route context.");
return registry.GetSerializer(contentType)
?? throw new InvalidOperationException(
$"Data format '{contentType}' is not registered. Register it with context.AddDataFormat(...) " +
"(a format package offers e.g. AddCsvDataFormat()) or pass the serializer instance: Marshal(new CsvDataFormat(...)).");
}
return (IMessageSerializer)Activator.CreateInstance(serializerType!)!;
}
}
///
/// Leaf definition that serializes the exchange body with an :
/// by type (Marshal<JsonMessageSerializer>()), by instance (Marshal(new CsvDataFormat(o)))
/// or by registered content type (Marshal("text/csv")).
///
public sealed class MarshalDefinition : ProcessorDefinition
{
private readonly Type? _serializerType;
private readonly IMessageSerializer? _serializer;
/// Serializer created from its parameterless constructor at route build.
public MarshalDefinition(Type serializerType)
{
ArgumentNullException.ThrowIfNull(serializerType);
_serializerType = serializerType;
}
/// A configured serializer instance (per-node options).
public MarshalDefinition(IMessageSerializer serializer)
{
ArgumentNullException.ThrowIfNull(serializer);
_serializer = serializer;
}
/// A content type resolved in the context's at route build.
public MarshalDefinition(string contentType)
{
ArgumentException.ThrowIfNullOrWhiteSpace(contentType);
ContentType = contentType;
}
/// Content type used for registry resolution, when the node was declared that way (XML form format=).
public string? ContentType { get; }
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new MarshalProcessor(DataFormatResolution.Resolve(context, _serializerType, _serializer, ContentType));
}
///
/// Leaf definition that deserializes the exchange body to with an
/// chosen by type, by instance or by registered content type.
///
public sealed class UnmarshalDefinition : ProcessorDefinition
{
private readonly Type? _serializerType;
private readonly IMessageSerializer? _serializer;
/// Serializer created from its parameterless constructor at route build.
public UnmarshalDefinition(Type serializerType, Type targetType)
{
ArgumentNullException.ThrowIfNull(serializerType);
ArgumentNullException.ThrowIfNull(targetType);
_serializerType = serializerType;
TargetType = targetType;
}
/// A configured serializer instance (per-node options).
public UnmarshalDefinition(IMessageSerializer serializer, Type targetType)
{
ArgumentNullException.ThrowIfNull(serializer);
ArgumentNullException.ThrowIfNull(targetType);
_serializer = serializer;
TargetType = targetType;
}
/// A content type resolved in the context's at route build.
public UnmarshalDefinition(string contentType, Type targetType)
{
ArgumentException.ThrowIfNullOrWhiteSpace(contentType);
ArgumentNullException.ThrowIfNull(targetType);
ContentType = contentType;
TargetType = targetType;
}
/// Content type used for registry resolution, when the node was declared that way (XML form format=).
public string? ContentType { get; }
/// CLR type the body is deserialized to.
public Type TargetType { get; }
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new UnmarshalProcessor(DataFormatResolution.Resolve(context, _serializerType, _serializer, ContentType), TargetType);
}
///
/// Leaf definition that converts the exchange body to a target type via
/// (resolved from the message content type) or the built-in conversions.
///
public sealed class ConvertBodyDefinition : ProcessorDefinition
{
private readonly Type _targetType;
/// Creates the node.
public ConvertBodyDefinition(Type targetType)
{
ArgumentNullException.ThrowIfNull(targetType);
_targetType = targetType;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ConvertBodyProcessor(_targetType, context.GetService());
}