Path: redb-route/redb.Route/Serialization/DataFormatRegistry.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/DataFormatRegistry.cs ---- using redb.Route.Abstractions; namespace redb.Route.Serialization; /// /// Default registry mapping content types to instances. /// Pre-registers JSON and XML serializers. Thread-safe: a format may be registered while routes /// are already resolving serializers (a hot-loaded module adding its own). /// public sealed class DataFormatRegistry : IDataFormatRegistry { private readonly System.Collections.Concurrent.ConcurrentDictionary _serializers = new(StringComparer.OrdinalIgnoreCase); private readonly System.Collections.Concurrent.ConcurrentDictionary _profiles = new(StringComparer.OrdinalIgnoreCase); /// Creates a registry with JSON and XML serializers pre-registered. public DataFormatRegistry() { var json = new JsonMessageSerializer(); _serializers["application/json"] = json; var xml = new XmlMessageSerializer(); _serializers["application/xml"] = xml; _serializers["text/xml"] = xml; // Byte wrappers shipped with the core (no dependencies): Marshal("application/gzip") etc. Register(Base64MessageSerializer.DefaultContentType, new Base64MessageSerializer()); Register(GZipMessageSerializer.DefaultContentType, new GZipMessageSerializer()); Register(ZipMessageSerializer.DefaultContentType, new ZipMessageSerializer()); // HTML form data, also without dependencies: ConvertBody() right after an HTTP consumer. Register(FormUrlEncodedMessageSerializer.DefaultContentType, new FormUrlEncodedMessageSerializer()); } /// public IMessageSerializer? GetSerializer(string contentType) { ArgumentNullException.ThrowIfNull(contentType); // Exact match if (_serializers.TryGetValue(contentType, out var s)) return s; // Strip parameters (e.g. "application/json; charset=utf-8" → "application/json") var semi = contentType.IndexOf(';'); if (semi > 0) { var baseType = contentType[..semi].Trim(); if (_serializers.TryGetValue(baseType, out s)) return s; } // Structured suffix: "application/vnd.api+json" → try "application/json" if (contentType.Contains("+json", StringComparison.OrdinalIgnoreCase)) return _serializers.GetValueOrDefault("application/json"); if (contentType.Contains("+xml", StringComparison.OrdinalIgnoreCase)) return _serializers.GetValueOrDefault("application/xml"); return null; } /// public void Register(string contentType, IMessageSerializer serializer) { ArgumentException.ThrowIfNullOrEmpty(contentType); ArgumentNullException.ThrowIfNull(serializer); // Primary registration by the caller-specified content type. _serializers[contentType] = serializer; // Also fan out to all media-type aliases the serializer declares, so a single // Register(...) call covers structured-suffix variants (e.g. "application/json" // + "application/problem+json" if both are claimed by the instance). foreach (var alias in serializer.MediaTypes) { if (!string.IsNullOrEmpty(alias) && !string.Equals(alias, contentType, StringComparison.OrdinalIgnoreCase)) { _serializers.TryAdd(alias, serializer); } } } /// public IMessageSerializer? ResolveProfile(string profileName) { ArgumentException.ThrowIfNullOrEmpty(profileName); return _profiles.GetValueOrDefault(profileName); } /// public void RegisterProfile(string profileName, IMessageSerializer serializer) { ArgumentException.ThrowIfNullOrEmpty(profileName); ArgumentNullException.ThrowIfNull(serializer); _profiles[profileName] = serializer; } }