Skip to content

Commit

Permalink
Merge #103: feat: new article: bencode to json converter
Browse files Browse the repository at this point in the history
b238981 feat: new article: bencode to json converter (Jose Celano)

Pull request description:

  New article: [Bencode](https://en.wikipedia.org/wiki/Bencode) to JSON converter.

ACKs for top commit:
  josecelano:
    ACK b238981

Tree-SHA512: 5ca810d49b7015ebf40d8f50d29ce3ad4a481ba7a367b8d7e9d7c74b56d6c60703199e02e70e84c719aaa6ba0e0d199f2aa27a82666389e4562cde3322e7403b
  • Loading branch information
josecelano committed Nov 1, 2024
2 parents f9b7008 + b238981 commit d70fd78
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
210 changes: 210 additions & 0 deletions src/routes/blog/bencode-to-json-converter-in-rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
---
title: Bencode to JSON Converter in Rust
slug: bencode-to-json-converter-in-rust
coverImage: /images/posts/bencode-to-json-converter-in-rust/bencode-to-json-converter-in-rust.webp
date: 2024-11-01T11:57:37.926Z
updated: 2024-11-01T11:57:37.926Z
excerpt: We're excited to introduce bencode2json, a crate that simplifies converting Bencode data to JSON, benefiting the Rust BitTorrent community.
contributor: Jose Celano
contributorSlug: jose-celano
tags:
- Bencode
- JSON
- Converter
- Rust
hidden: false
---

<script>
import Callout from "$lib/components/molecules/Callout.svelte";
import CodeBlock from "$lib/components/molecules/CodeBlock.svelte";
import Image from "$lib/components/atoms/Image.svelte";
import PostBody from "$lib/components/molecules/PostBody.svelte";
import PostContainer from "$lib/components/molecules/PostContainer.svelte";
import PostTable from "$lib/components/molecules/PostTable.svelte";
import TableOfContents from '$lib/components/atoms/TableOfContents.svelte';

let sections = [
{ name: "Introduction", id: "introduction" },
{ name: "Why It Is Useful", id: "why-it-is-useful" },
{ name: "How You Can Use It", id: "how-you-can-use-it" },
{ name: "Performance", id: "performance" },
{ name: "How You Can Contribute", id: "how-you-can-contribute" },
{ name: "Conclusion", id: "conclusion" },
{ name: "Acknowledgments", id: "acknowledgments" }
]

let activeSection = '';
</script>

<PostContainer>
<PostTable>

## Table of contents

<TableOfContents {sections} {activeSection} />

</PostTable>

<PostBody>

## Introduction

Hello Bittorrent-in-Rust Community!

At Torrust, we're committed to enhancing the BitTorrent ecosystem within the Rust community. Today, we're excited to introduce a Bencode to JSON converter:

[bencode2json](https://github.com/torrust/bencode2json)

It's a new crate designed to simplify the conversion of Bencode data into JSON format. This tool not only supports our Tracker but also aims to benefit the wider Rust community involved in BitTorrent projects.

Here’s a quick example to show how straightforward it is to use:

<CodeBlock lang="terminal">

```s
echo "4:spam" | cargo run
"spam"
```

</CodeBlock>

For non UTF-8 Bencoded strings, we represent them as byte sequences enclosed in HTML-styled tags `<hex>...</hex>`;

<CodeBlock lang="terminal">

```s
printf "d3:bar2:\xFF\xFEe" | cargo run
{"bar":"<hex>fffe</hex>"}
```

</CodeBlock>

## Why It Is Useful

Bencode is the encoding format used by BitTorrent, while JSON is a widely-used data interchange format. Converting Bencode to JSON can facilitate easier data manipulation, debugging, and integration with modern web applications. Here are a few reasons why `bencode2json` is valuable:

- **Interoperability**: JSON is supported by most programming languages and libraries, making it easier to work with Bencoded data in diverse environments.
- **Readability**: JSON is often easier to read and understand compared to Bencode, especially for developers unfamiliar with the latter.
- **Integration**: Many web services and APIs utilize JSON, so having Bencode data readily available in this format can simplify integrations and data exchanges.

## How You Can Use It

Using `bencode2json` is straightforward. Here's a quick guide on how to get started:

### Installation

Add the crate to your `Cargo.toml`:

<CodeBlock lang="toml">

```toml
[dependencies]
bencode2json = "0.1.0"
```

</CodeBlock>

### Basic Usage

Here's a simple example of how to convert Bencode to JSON:

<CodeBlock lang="rust">

```rust
use bencode2json::try_bencode_to_json;

fn main() {
use torrust_bencode2json::{try_bencode_to_json};

let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();

assert_eq!(result, r#"{"spam":"eggs"}"#);
}
```

</CodeBlock>

### Advance Usage

`bencode2json` supports any kind of input and output that implement Read or Write traits,
meaning you can read from stdin and write to stdout or from/to files.

<CodeBlock lang="rust">

```rust
use bencode2json::parsers::{BencodeParser};

let mut output = String::new();

let mut parser = BencodeParser::new(&b"4:spam"[..]);

parser
.write_str(&mut output)
.expect("Bencode to JSON conversion failed");

println!("{output}"); // It prints the JSON string: "spam"
```

</CodeBlock>

### Using the Console App

The package provides a console command too. You can install it with:

<CodeBlock lang="terminal">

```s
cargo install torrust-bencode2json
```

</CodeBlock>

And execute it to convert a torrent file into JSON.

<CodeBlock lang="terminal">

```s
cat ./sample.torrent | cargo run | jq
```

</CodeBlock>

[jq](https://jqlang.github.io/jq/) is jq is a lightweight and flexible command-line JSON processor. Make sure you have it installed.

### Performance

The converter only generates temporary in-memory representations for strings, ensuring memory consumption is proportional to the size of the largest Bencoded string. This design makes bencode2json efficient for both small and large datasets.

### How You Can Contribute

We're excited to see how the community can help improve bencode2json. Here are some ways you can get involved:

- Feature Requests: If you have ideas for features or improvements, please open an issue on the GitHub repository.
- Code Contributions: Help us build an opposite converter, JSON to Bencode, by submitting a pull request. This could greatly enhance the functionality of the crate and provide a complete solution for working with these data formats.
- Documentation: Contributing to the documentation is always valuable. Clear examples and explanations help other users understand and utilize the crate effectively.
- Feedback: Share your experience using bencode2json. Your feedback will help us make it even better!

## Conclusion

`bencode2json` is a step forward in bridging the gap between Bencode and JSON within the BitTorrent ecosystem in Rust. We encourage everyone to try it out, contribute, and join us in fostering a collaborative environment for Rust developers. Happy coding!

Check out the [bencode2json GitHub repository](https://github.com/torrust/bencode2json) to get started and contribute to the project. Let's make working with BitTorrent in Rust easier together!

## Acknowledgments

This implementation is basically a port to Rust from <https://gist.github.com/camilleoudot/840929699392b3d25afbec25d850c94a> with some changes like:

- It does not use magic numbers (explicit enum for states).
- It prints non UTF-8 string in hexadecimal.

The idea of using hexadecimal format `<hex>ff</hex>` for non UTF-8 string came from the bencode online repo by [@Chocobo1](https://github.com/Chocobo1).

We also want to thank [@da2ce7](https://github.com/da2ce7) for his feedback and review that has improved this project significantly.

If you have any questions or issues regarding this post, please [open an issue](https://github.com/torrust/bencode2json/issues/new).

We very welcome any contributions to the project!

</PostBody>
</PostContainer>
Binary file not shown.

0 comments on commit d70fd78

Please sign in to comment.