rustHow to read JSON file in Rust
Reading a JSON file in Rust is relatively straightforward. First, you need to use the serde_json
crate to parse the JSON file. Then, you can use the serde::Deserialize
trait to deserialize the JSON data into a Rust struct. Finally, you can use the serde::Serialize
trait to serialize the data back into a JSON string. The following ## Code example shows how to read a JSON file and deserialize it into a Rust struct:
use serde::{Deserialize, Serialize};
use serde_json::Result;
#[derive(Serialize, Deserialize)]
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
fn main() -> Result<()> {
// Open the file in read-only mode with buffer.
let file = std::fs::File::open("person.json")?;
let reader = std::io::BufReader::new(file);
// Read the JSON contents of the file as an instance of `Person`.
let p: Person = serde_json::from_reader(reader)?;
println!("{} is {} years old", p.name, p.age);
// Convert `Person` back to a JSON string.
let j = serde_json::to_string(&p)?;
println!("{}", j);
Ok(())
}
The output of the above ## Code example would be:
John is 30 years old
{"name":"John","age":30,"phones":["555-1234","555-2345"]}
The serde_json
crate provides the serde::Deserialize
and serde::Serialize
traits which allow you to easily deserialize and serialize JSON data into Rust structs. The serde_json::from_reader
function is used to read the JSON file and deserialize it into a Rust struct. The serde_json::to_string
function is used to serialize the data back into a JSON string.
Helpful links
Related
- How to write struct to file in Rust
- How to write buffer to file in Rust
- How to write string to file in Rust
- How to write bytes to file in Rust
- How to write line to file in Rust
- How to write to file in Rust
- How to read binary file in Rust
- How to read all lines from file in Rust
- How to append to file in Rust
More of Rust
- How to use non-capturing groups in Rust regex?
- How to implement PartialEq for a Rust HashMap?
- How to replace a capture group using Rust regex?
- How to use regex to match a double quote in Rust?
- How to use regex to match a group in Rust?
- How to parse JSON string in Rust?
- How to get a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to use regex with bytes in Rust?
- Hashshet example in Rust
See more codes...