Path: redb-route/redb.Route/Serialization/JsonMessageSerializer.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/JsonMessageSerializer.cs ---- using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Serialization; using redb.Route.Abstractions; namespace redb.Route.Serialization; /// /// JSON message serializer using System.Text.Json. /// Thread-safe, singleton-friendly. Configurable via . /// public sealed class JsonMessageSerializer : IMessageSerializer { private readonly JsonSerializerOptions _options; private readonly string _contentType; private readonly IReadOnlyCollection _mediaTypes; /// /// Default options: camelCase, case-insensitive, ignore null values on write, UTF-8. /// Uses so non-ASCII /// (Cyrillic, emoji, diacritics) and ASCII punctuation like " are emitted /// as-is in UTF-8 instead of being escaped to \u0022/\u0410. /// This is safe for HTTP API responses; only unsafe when embedding JSON inside /// HTML/JS inline — which message-serializer output never does. /// Exotic transports (e.g. JSONP, HTML-inline) can override via constructor. /// public static readonly JsonSerializerOptions DefaultOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNameCaseInsensitive = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, WriteIndented = false, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; // The static options build metadata over every message type; a module's type in them is let go on unload. static JsonMessageSerializer() => Core.CollectibleContextCleanup.EnsureWatching(); /// Creates a JSON serializer with default options. public JsonMessageSerializer() : this(DefaultOptions, contentType: "application/json", mediaTypes: null) { } /// Creates a JSON serializer with custom options. /// JSON serializer options. public JsonMessageSerializer(JsonSerializerOptions options) : this(options, contentType: "application/json", mediaTypes: null) { } /// /// Creates a JSON serializer with custom options, a specific primary content type, /// and an optional extended list of media-type aliases the instance also handles. /// /// JSON serializer options. /// Primary content type reported by /// (e.g. application/scim+json). /// Optional alias list reported by /// . If null, defaults to [contentType]. /// /// Used to ship profile serializers (SCIM, Problem Details, …) that own their own /// and declare non-application/json /// media types without subclassing. /// public JsonMessageSerializer( JsonSerializerOptions options, string contentType, IReadOnlyCollection? mediaTypes) { _options = options ?? throw new ArgumentNullException(nameof(options)); _contentType = !string.IsNullOrEmpty(contentType) ? contentType : throw new ArgumentException("Content type must not be empty.", nameof(contentType)); _mediaTypes = mediaTypes is { Count: > 0 } ? mediaTypes : new[] { _contentType }; } /// public string ContentType => _contentType; /// public IReadOnlyCollection MediaTypes => _mediaTypes; /// public byte[] Serialize(T value) { return JsonSerializer.SerializeToUtf8Bytes(value, _options); } /// public T? Deserialize(byte[] data) { ArgumentNullException.ThrowIfNull(data); return JsonSerializer.Deserialize(data, _options); } /// public object? Deserialize(byte[] data, Type type) { ArgumentNullException.ThrowIfNull(data); ArgumentNullException.ThrowIfNull(type); return JsonSerializer.Deserialize(data, type, _options); } }