Bodu.Text.Bencode 0.7.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bodu.Text.Bencode --version 0.7.0
                    
NuGet\Install-Package Bodu.Text.Bencode -Version 0.7.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bodu.Text.Bencode" Version="0.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bodu.Text.Bencode" Version="0.7.0" />
                    
Directory.Packages.props
<PackageReference Include="Bodu.Text.Bencode" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bodu.Text.Bencode --version 0.7.0
                    
#r "nuget: Bodu.Text.Bencode, 0.7.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bodu.Text.Bencode@0.7.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bodu.Text.Bencode&version=0.7.0
                    
Install as a Cake Addin
#tool nuget:?package=Bodu.Text.Bencode&version=0.7.0
                    
Install as a Cake Tool

Bodu.Text.Bencode

API stability — Stable. The public API surface is committed; breaking changes are reserved for a major-version bump per SemVer.

A Bencode (BEP 3) serializer for .NET 8. It maps plain CLR objects to and from Bencode through a configurable converter model, over a low-level, forward-only token reader and writer.

Installation

dotnet add package Bodu.Text.Bencode

Targets net8.0.

API shape

The public surface layers a high-level serializer, two document object models, and a low-level token reader/writer pair:

Type(s) Namespace Role
BencodeSerializer Bodu.Text.Bencode Static entry point: Serialize / Deserialize between CLR objects and Bencode.
BencodeSerializerOptions Bodu.Text.Bencode Serializer configuration: converters, naming policy, ignore conditions, depth.
Utf8BencodeReader / Utf8BencodeWriter Bodu.Text.Bencode Forward-only, allocation-free ref struct token reader and writer.
BencodeTokenType Bodu.Text.Bencode Classifies the token the reader is positioned on.
BencodeNamingPolicy Bodu.Text.Bencode Converts member names to wire keys (for example camel case).
BencodeConverter<T> / BencodeConverterFactory Bodu.Text.Bencode.Serialization Custom per-type read/write logic plugged into the serializer.
[BencodePropertyName] / [BencodeIgnore] Bodu.Text.Bencode.Serialization Per-member attributes controlling wire names and inclusion.
BencodeDocument / BencodeElement Bodu.Text.Bencode.Document Read-only, low-allocation document object model.
BencodeNode / BencodeObject / BencodeArray / BencodeValue Bodu.Text.Bencode.Nodes Mutable document object model: parse, edit, write back.
using Bodu.Text.Bencode;

byte[] payload = BencodeSerializer.Serialize(new TorrentInfo { Name = "ubuntu.iso", Length = 1024 });
TorrentInfo info = BencodeSerializer.Deserialize<TorrentInfo>(payload);
  • Serialize / Deserialize over byte[], ReadOnlySpan<byte>, IBufferWriter<byte>, and Stream (synchronous and asynchronous), plus SerializeToNode, SerializeToDocument, and Deserialize(BencodeNode) bridges into the two DOMs.
  • Output is always canonical Bencode: dictionary entries are emitted in ascending bytewise key order, and the writer rejects duplicate keys and (by default) a second root value.
  • Strings and byte[] map to byte strings, the integer family to i…e, and enums to member-name byte strings. Types with no canonical Bencode form (Booleans, floating-point, date-times) require a registered BencodeConverter<T>; a null member is omitted on write.
  • Utf8BencodeReader and Utf8BencodeWriter expose the low-level token surface directly for callers that do not want POCO mapping, including ValueTextEquals, CopyString, TokenStartIndex, the width-checked integer accessors, WriteRawValue, and the combined property-and-value overloads (writer.WriteInteger("length", 42)). By default the reader accepts only canonical BEP 3 (no leading or negative zeros, ascending unique dictionary keys, a single root with no trailing bytes).
  • BencodeElement.GetRawBytes() returns a value's exact encoded slice — for example the info dictionary of a torrent, whose SHA-1 is the info-hash — and WriteRawValue re-emits such slices verbatim.

Contracts and limits

Integers. BEP 3 integers are arbitrary-precision; this library supports the range [long.MinValue, ulong.MaxValue] on every surface. Values in (long.MaxValue, ulong.MaxValue] are readable through Utf8BencodeReader.GetUInt64, BencodeElement.GetUInt64, and GetValue<ulong>() on nodes, and writable through the ulong overload of WriteInteger and BencodeValue.Create(ulong); anything outside the supported range is rejected with BencodeFormatException. Arbitrary-precision (BigInteger) values are not supported.

Byte strings are bytes, not text. GetString accessors (reader, element, node) and string-typed members decode as UTF-8 and substitute U+FFFD for invalid sequences. Binding a binary field — such as a torrent's pieces — to a string silently corrupts it; map binary content to byte[] (or read ValueSpan / GetBytes), which is always lossless.

Nesting depth. Utf8BencodeReader, Utf8BencodeWriter, and BencodeDocument default to a maximum depth of 256; BencodeSerializerOptions.MaxDepth defaults to 64 because the serializer is the typical entry point for untrusted input. All four are configurable.

Single root. A Bencode document is a single value. The reader rejects trailing bytes, and the writer rejects a second top-level value unless BencodeWriterOptions.AllowMultipleRootValues opts into concatenated-value framings.

Lenient reading of real-world documents. Older encoders occasionally emit unsorted or duplicate dictionary keys. AllowUnsortedKeys and AllowDuplicateKeys — available on BencodeReaderOptions, BencodeDocumentOptions, and BencodeSerializerOptions — relax those two rules independently while everything else stays strict. With duplicates permitted, the document model returns the first occurrence from name lookups (enumeration shows every pair), while the node tree and the serializer bind last-wins. Writing is always strict.

Exceptions. Failures are split by cause: BencodeFormatException (a FormatException, carrying the byte Offset) reports malformed input, and BencodeSerializationException reports values or documents that cannot be mapped. Catch both when handling should not distinguish the cause.

Property-name matching. BencodeSerializerOptions.PropertyNameCaseInsensitive defaults to true, so reads bind wire keys to members leniently. Wire keys themselves are raw bytes and case-sensitive; output never changes case.

Runnable samples

The repository ships an offline, dotnet run-able sample for this package — a real BitTorrent metainfo file read, verified, and re-authored end to end (DOM inspection, canonical byte-exact round trips, the raw-slice info-hash, typed POCO mapping) — under samples/Text.Bencode/.

Testing

Tests live in test/ as MSTest classes mirroring src/. Run tiers via the runsettings files at the solution root:

dotnet test Bodu.Text.Bencode/test/Bodu.Text.Bencode.Test.csproj --settings bvt.runsettings
dotnet test Bodu.Text.Bencode/test/Bodu.Text.Bencode.Test.csproj --settings regression.runsettings

License

MIT. © Bodu Pty. Ltd.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bodu.Text.Bencode:

Package Downloads
Bodu.Extensions.Configuration.Text

The EditorConfig-compatible Microsoft.Extensions.Configuration provider. Bridges Bodu.Text.Configuration into the standard IConfiguration / IOptions<T> pipeline so a .boduconfig or bodu.config file registers with the same AddTextConfiguration(...) shape used for the JSON, INI, and XML providers — with file, stream, and programmatic-document overloads, reload-on-change, and colon-delimited section binding. Also ships the read-only TOML (AddTomlFile / AddTomlStream) and Bencode (AddBencodeFile / AddBencodeStream) sources.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 51 9/24/2026
0.7.0 92 9/24/2026
0.6.0 64 9/24/2026
0.5.0 63 9/23/2026