Skip to content

Commit d70fd78

Browse files
committed
Merge #103: feat: new article: bencode to json converter
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
2 parents f9b7008 + b238981 commit d70fd78

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
title: Bencode to JSON Converter in Rust
3+
slug: bencode-to-json-converter-in-rust
4+
coverImage: /images/posts/bencode-to-json-converter-in-rust/bencode-to-json-converter-in-rust.webp
5+
date: 2024-11-01T11:57:37.926Z
6+
updated: 2024-11-01T11:57:37.926Z
7+
excerpt: We're excited to introduce bencode2json, a crate that simplifies converting Bencode data to JSON, benefiting the Rust BitTorrent community.
8+
contributor: Jose Celano
9+
contributorSlug: jose-celano
10+
tags:
11+
- Bencode
12+
- JSON
13+
- Converter
14+
- Rust
15+
hidden: false
16+
---
17+
18+
<script>
19+
import Callout from "$lib/components/molecules/Callout.svelte";
20+
import CodeBlock from "$lib/components/molecules/CodeBlock.svelte";
21+
import Image from "$lib/components/atoms/Image.svelte";
22+
import PostBody from "$lib/components/molecules/PostBody.svelte";
23+
import PostContainer from "$lib/components/molecules/PostContainer.svelte";
24+
import PostTable from "$lib/components/molecules/PostTable.svelte";
25+
import TableOfContents from '$lib/components/atoms/TableOfContents.svelte';
26+
27+
let sections = [
28+
{ name: "Introduction", id: "introduction" },
29+
{ name: "Why It Is Useful", id: "why-it-is-useful" },
30+
{ name: "How You Can Use It", id: "how-you-can-use-it" },
31+
{ name: "Performance", id: "performance" },
32+
{ name: "How You Can Contribute", id: "how-you-can-contribute" },
33+
{ name: "Conclusion", id: "conclusion" },
34+
{ name: "Acknowledgments", id: "acknowledgments" }
35+
]
36+
37+
let activeSection = '';
38+
</script>
39+
40+
<PostContainer>
41+
<PostTable>
42+
43+
## Table of contents
44+
45+
<TableOfContents {sections} {activeSection} />
46+
47+
</PostTable>
48+
49+
<PostBody>
50+
51+
## Introduction
52+
53+
Hello Bittorrent-in-Rust Community!
54+
55+
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:
56+
57+
[bencode2json](https://github.com/torrust/bencode2json)
58+
59+
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.
60+
61+
Here’s a quick example to show how straightforward it is to use:
62+
63+
<CodeBlock lang="terminal">
64+
65+
```s
66+
echo "4:spam" | cargo run
67+
"spam"
68+
```
69+
70+
</CodeBlock>
71+
72+
For non UTF-8 Bencoded strings, we represent them as byte sequences enclosed in HTML-styled tags `<hex>...</hex>`;
73+
74+
<CodeBlock lang="terminal">
75+
76+
```s
77+
printf "d3:bar2:\xFF\xFEe" | cargo run
78+
{"bar":"<hex>fffe</hex>"}
79+
```
80+
81+
</CodeBlock>
82+
83+
## Why It Is Useful
84+
85+
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:
86+
87+
- **Interoperability**: JSON is supported by most programming languages and libraries, making it easier to work with Bencoded data in diverse environments.
88+
- **Readability**: JSON is often easier to read and understand compared to Bencode, especially for developers unfamiliar with the latter.
89+
- **Integration**: Many web services and APIs utilize JSON, so having Bencode data readily available in this format can simplify integrations and data exchanges.
90+
91+
## How You Can Use It
92+
93+
Using `bencode2json` is straightforward. Here's a quick guide on how to get started:
94+
95+
### Installation
96+
97+
Add the crate to your `Cargo.toml`:
98+
99+
<CodeBlock lang="toml">
100+
101+
```toml
102+
[dependencies]
103+
bencode2json = "0.1.0"
104+
```
105+
106+
</CodeBlock>
107+
108+
### Basic Usage
109+
110+
Here's a simple example of how to convert Bencode to JSON:
111+
112+
<CodeBlock lang="rust">
113+
114+
```rust
115+
use bencode2json::try_bencode_to_json;
116+
117+
fn main() {
118+
use torrust_bencode2json::{try_bencode_to_json};
119+
120+
let result = try_bencode_to_json(b"d4:spam4:eggse").unwrap();
121+
122+
assert_eq!(result, r#"{"spam":"eggs"}"#);
123+
}
124+
```
125+
126+
</CodeBlock>
127+
128+
### Advance Usage
129+
130+
`bencode2json` supports any kind of input and output that implement Read or Write traits,
131+
meaning you can read from stdin and write to stdout or from/to files.
132+
133+
<CodeBlock lang="rust">
134+
135+
```rust
136+
use bencode2json::parsers::{BencodeParser};
137+
138+
let mut output = String::new();
139+
140+
let mut parser = BencodeParser::new(&b"4:spam"[..]);
141+
142+
parser
143+
.write_str(&mut output)
144+
.expect("Bencode to JSON conversion failed");
145+
146+
println!("{output}"); // It prints the JSON string: "spam"
147+
```
148+
149+
</CodeBlock>
150+
151+
### Using the Console App
152+
153+
The package provides a console command too. You can install it with:
154+
155+
<CodeBlock lang="terminal">
156+
157+
```s
158+
cargo install torrust-bencode2json
159+
```
160+
161+
</CodeBlock>
162+
163+
And execute it to convert a torrent file into JSON.
164+
165+
<CodeBlock lang="terminal">
166+
167+
```s
168+
cat ./sample.torrent | cargo run | jq
169+
```
170+
171+
</CodeBlock>
172+
173+
[jq](https://jqlang.github.io/jq/) is jq is a lightweight and flexible command-line JSON processor. Make sure you have it installed.
174+
175+
### Performance
176+
177+
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.
178+
179+
### How You Can Contribute
180+
181+
We're excited to see how the community can help improve bencode2json. Here are some ways you can get involved:
182+
183+
- Feature Requests: If you have ideas for features or improvements, please open an issue on the GitHub repository.
184+
- 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.
185+
- Documentation: Contributing to the documentation is always valuable. Clear examples and explanations help other users understand and utilize the crate effectively.
186+
- Feedback: Share your experience using bencode2json. Your feedback will help us make it even better!
187+
188+
## Conclusion
189+
190+
`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!
191+
192+
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!
193+
194+
## Acknowledgments
195+
196+
This implementation is basically a port to Rust from <https://gist.github.com/camilleoudot/840929699392b3d25afbec25d850c94a> with some changes like:
197+
198+
- It does not use magic numbers (explicit enum for states).
199+
- It prints non UTF-8 string in hexadecimal.
200+
201+
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).
202+
203+
We also want to thank [@da2ce7](https://github.com/da2ce7) for his feedback and review that has improved this project significantly.
204+
205+
If you have any questions or issues regarding this post, please [open an issue](https://github.com/torrust/bencode2json/issues/new).
206+
207+
We very welcome any contributions to the project!
208+
209+
</PostBody>
210+
</PostContainer>
415 KB
Loading

0 commit comments

Comments
 (0)