Path: redb-route/redb.Route/Serialization/FormUrlEncodedMessageSerializer.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/FormUrlEncodedMessageSerializer.cs ---- namespace redb.Route.Serialization; /// /// HTML form data as a data format, registered by default: /// Unmarshal<FormBody>("application/x-www-form-urlencoded") or ConvertBody<FormBody>() after an /// HTTP consumer reads the posted fields, Marshal("application/x-www-form-urlencoded") writes a /// or a dictionary of strings. Parsing and encoding follow the WHATWG URL standard. /// /// /// Unmarshal targets: (every field, in order) and Dictionary<string, string> /// (the first value of each name). Marshal sources: and any sequence of string pairs, such as /// IDictionary<string, string>. /// public sealed class FormUrlEncodedMessageSerializer : Abstractions.IMessageSerializer { /// Content type the format is registered under by default. public const string DefaultContentType = "application/x-www-form-urlencoded"; private readonly FormUrlEncodedOptions _options; /// A format with the default options (UTF-8, ASP.NET Core limits). public FormUrlEncodedMessageSerializer() : this(new FormUrlEncodedOptions()) { } /// A format with the given options. public FormUrlEncodedMessageSerializer(FormUrlEncodedOptions options) { _options = options ?? throw new ArgumentNullException(nameof(options)); ArgumentNullException.ThrowIfNull(options.Encoding, nameof(options.Encoding)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(options.MaxFields, nameof(options.MaxFields)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(options.MaxNameLength, nameof(options.MaxNameLength)); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(options.MaxValueLength, nameof(options.MaxValueLength)); } /// A format configured in place. public FormUrlEncodedMessageSerializer(Action configure) : this(Configure(configure)) { } /// public string ContentType => DefaultContentType; /// public byte[] Serialize(T value) => value switch { null => [], IEnumerable> fields => _options.Encoding.GetBytes(FormUrlEncoding.Encode(fields, _options.Encoding)), _ => throw new NotSupportedException( $"Form data cannot be written from '{value.GetType().FullName}': use FormBody or a sequence of string pairs (IDictionary).") }; /// public T? Deserialize(byte[] data) => (T?)Deserialize(data, typeof(T)); /// public object? Deserialize(byte[] data, Type type) { ArgumentNullException.ThrowIfNull(data); ArgumentNullException.ThrowIfNull(type); var form = FormUrlEncoding.Decode(data, _options.Encoding, _options); if (type.IsAssignableFrom(typeof(FormBody))) return form; if (type.IsAssignableFrom(typeof(Dictionary))) return form.ToDictionary(); throw new NotSupportedException( $"Form data cannot be read as '{type.FullName}': use FormBody (every field) or Dictionary (the first value of each name)."); } private static FormUrlEncodedOptions Configure(Action configure) { ArgumentNullException.ThrowIfNull(configure); var options = new FormUrlEncodedOptions(); configure(options); return options; } }