How to properly parse JSON from a file in Rust using serde?
Hi everyone! I just started learning Rust and encountered a typical task: I need to read a JSON file and convert it into a data structure. I have a `config.json` file with roughly this content:
```json
{
"name": "MyApp",
"version": 1,
"debug": true,
"dependencies": ["serde", "tokio"]
}
```
I added the `serde` and `serde_json` crates to `Cargo.toml`, wrote the struct, but I can't figure out how to properly read the file and parse it. I tried this:
```rust
use serde::Deserialize;
use std::fs::File;
use std::io::Read;
#[derive(Deserialize)]
struct Config {
name: String,
version: u32,
debug: bool,
dependencies: Vec,
}
fn main() {
let mut file = File::open("config.json").unwrap();
let mut data = String::new();
file.
No answers yet.
Log in to leave an answer
Blogs