Path: redb-route/redb.Route/Extensions/RedbRouteCodecExtensions.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/Extensions/RedbRouteCodecExtensions.cs
----
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.DependencyInjection;
using redb.Route.Abstractions;
using redb.Route.Configuration;
using redb.Route.Core;
using redb.Route.Serialization;
namespace redb.Route.Extensions;
///
/// Fluent extensions for configuring message codecs on a .
///
/// These extensions operate on , which lives inside
/// (not the DI container). Registration is deferred via
/// so it is applied at context-start time.
///
///
public static class RedbRouteCodecExtensions
{
///
/// Replaces the default application/json codec in the route context's
/// with one built from the supplied action.
///
/// Route builder.
/// Action applied to a fresh
/// instance (already seeded with
/// and case-insensitive property matching). Mutate to taste.
///
/// Profile-scoped codecs (SCIM, Problem Details, …) registered by other modules
/// (e.g. AddRedbIdentityServer) are not affected — this tunes only the
/// generic application/json entry.
///
public static RedbRouteBuilder ConfigureJsonCodec(
this RedbRouteBuilder builder,
Action configure)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(configure);
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
configure(options);
builder.Services.AddSingleton(
new JsonCodecConfigurator(options));
return builder;
}
///
/// Applies a bound instance to the default
/// application/json codec. Callers typically populate the options from
/// IConfiguration.GetSection("redbRoute:Codecs:Json").Get<JsonCodecOptions>()
/// in their host composition root.
///
/// Route builder.
/// Bound codec options; may not be null.
public static RedbRouteBuilder ConfigureJsonCodec(
this RedbRouteBuilder builder,
JsonCodecOptions options)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(options);
return builder.ConfigureJsonCodec(jso =>
{
jso.PropertyNamingPolicy = ParseNamingPolicy(options.PropertyNamingPolicy);
jso.PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive;
jso.DefaultIgnoreCondition = options.IgnoreNullValues
? JsonIgnoreCondition.WhenWritingNull
: JsonIgnoreCondition.Never;
jso.WriteIndented = options.WriteIndented;
jso.Encoder = options.UnsafeRelaxedEscaping
? JavaScriptEncoder.UnsafeRelaxedJsonEscaping
: JavaScriptEncoder.Default;
});
}
private static JsonNamingPolicy? ParseNamingPolicy(string? name) => name switch
{
null => null,
"" => null,
_ when string.Equals(name, "None", StringComparison.OrdinalIgnoreCase) => null,
_ when string.Equals(name, "CamelCase", StringComparison.OrdinalIgnoreCase) => JsonNamingPolicy.CamelCase,
_ when string.Equals(name, "SnakeCaseLower", StringComparison.OrdinalIgnoreCase) => JsonNamingPolicy.SnakeCaseLower,
_ when string.Equals(name, "SnakeCaseUpper", StringComparison.OrdinalIgnoreCase) => JsonNamingPolicy.SnakeCaseUpper,
_ when string.Equals(name, "KebabCaseLower", StringComparison.OrdinalIgnoreCase) => JsonNamingPolicy.KebabCaseLower,
_ when string.Equals(name, "KebabCaseUpper", StringComparison.OrdinalIgnoreCase) => JsonNamingPolicy.KebabCaseUpper,
_ => throw new ArgumentException(
$"Unknown JSON naming policy '{name}'. Supported: None, CamelCase, SnakeCaseLower, SnakeCaseUpper, KebabCaseLower, KebabCaseUpper.")
};
private sealed class JsonCodecConfigurator : IRouteContextConfigurator
{
private readonly JsonSerializerOptions _options;
public JsonCodecConfigurator(JsonSerializerOptions options) => _options = options;
public void Configure(RouteContext context)
{
var registry = context.GetService()
?? throw new InvalidOperationException(
"IDataFormatRegistry is not available on RouteContext. " +
"Ensure AddRedbRoute() was called before ConfigureJsonCodec().");
// Replace the generic application/json entry. Media-type aliases fan out
// automatically via DataFormatRegistry.Register + MediaTypes contract.
registry.Register("application/json", new JsonMessageSerializer(_options));
}
}
}