Bodu.Text.Bencode.Serialization Namespace
- Package
-
Bodu.Text.Bencode 1.0.0
Purpose
Bodu.Text.Bencode.Serialization holds the extensibility surface of the BencodeSerializer: the converter base types you derive from to control how a CLR type is written and read, and the built-in enum converters. A converter reads through the Utf8BencodeReader and writes through the Utf8BencodeWriter. The attribute family, naming policies, and serialization callbacks it works with are shared across formats and live in Bodu.Text.Serialization.
Key types
- BencodeConverter - the non-generic root (
CanConvert) thatBencodeSerializerOptions.Convertersholds. - BencodeConverter<T> - the per-type base: implement
ReadandWriteforT. - BencodeConverterFactory - the base for converter families:
CanConvertplusCreateConverterfor a closed type. - BencodeStringEnumConverter / BencodeStringEnumConverter<TEnum> - enums as member-name byte strings, with an optional NamingPolicy and integer fallback.
- BencodeNumberEnumConverter<TEnum> - enums as their underlying integer.
Example
using Bodu.Text.Bencode;
using Bodu.Text.Bencode.Reader;
using Bodu.Text.Bencode.Serialization;
using Bodu.Text.Bencode.Writer;
public sealed class VersionConverter : BencodeConverter<Version>
{
public override Version Read(ref Utf8BencodeReader reader, Type typeToConvert, BencodeSerializerOptions options) =>
Version.Parse(reader.GetString());
public override void Write(Utf8BencodeWriter writer, Version value, BencodeSerializerOptions options) =>
writer.WriteString(value.ToString());
}
var options = new BencodeSerializerOptions();
options.Converters.Add(new VersionConverter());
Notes
- Resolution order. A
[Converter]attribute on the member or type wins, thenConvertersin registration order, then the built-in catalog. - Kinds Bencode cannot represent. Booleans, floating-point values, and date-times have no built-in mapping - a registered converter must reduce them to an integer or byte string.
- Internal converters. The
Bodu.Text.Bencode.Serialization.Converterssub-namespace is internal; extend through the public base types above. - See also: the Bodu.Text.Bencode introduction and the writing converters guide.
Classes
- BencodeConverter
Serves as the non-generic base for every Bencode converter. The hierarchy is closed to two extension points: BencodeConverter<T>, which converts a single type, and BencodeConverterFactory, which produces a converter for a family of types.
- BencodeConverterFactory
Produces a BencodeConverter for a family of types that cannot be served by a single BencodeConverter<T> - for example every Nullable<T>, every enumeration, or every collection.
- BencodeConverter<T>
Converts a value of type
Tto and from the Bencode reader and writer. This is the base authors derive to customize how a specific type is serialized.
- BencodeNumberEnumConverter<TEnum>
Produces a converter that serializes the enumeration
TEnumas a Bencode integer carrying its underlying numeric value.
- BencodeStringEnumConverter
Produces a converter that serializes any enumeration as a Bencode byte string holding its member name, applying an optional naming policy and honoring StringEnumMemberNameAttribute on individual members.
- BencodeStringEnumConverter<TEnum>
Produces a converter that serializes the enumeration
TEnumas a Bencode byte string holding its member name, applying an optional naming policy and honoring StringEnumMemberNameAttribute on individual members.