A BitTorrent metadata file (.torrent) is a Bencoded Dictionary. The file is basically a dictionary of data like the Tracker URL and File Pieces which a BitTorrent client can parse. Bencode is a simple ‘cross-platform safe’ encoding that the (.torrent) files use so that they can be parsed across multiple OS platforms.

Bencode2Json is a .NET Core library that can be used to convert Bencoded Dictionaries to JSON Documents. You can then query the JSON directly or use any JSON frameworks like JSON.NET and convert it into strongly typed objects.

using Bencode2Json;
...
...
// BencodedData takes any Stream
var bencodedData = new BencodedData(dataStream);
var json = bencodedData.ConvertToJson();

Bencode2Json is packaged as a NuGet package and is available in the Nuget Gallery

You can install it from the Package Manager Console as follows

PM> Install-Package Bencode2Json

The source code is available on GitHub and is open for contribution. Feel free to send in your code contributions via a Pull Request or raise an Issue for suggestions.

A detailed example of parsing a .torrent file is available in the Examples folder.

Build status CodeFactor NuGet version