Function json_with_comments::de::from_read
source · pub fn from_read<R, D>(read: R) -> Result<D>where
R: Read,
D: DeserializeOwned,
Expand description
Deserialize a JSON with comments text from the given reader as type D
.
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct Product {
name: String,
price: u32,
}
let read = r#"
{
"name": "candy",
"price": 100
}"#.as_bytes();
let product: Product = json_with_comments::from_read(read).unwrap();
assert_eq!(product.name, "candy");
assert_eq!(product.price, 100);
§Errors
This function cannot deserialize string value as borrowed &str
.
It cause compile time error.
If you want to deserialize string value as escaped borrowed &str
, use from_str_raw
instead.
If you want to deserialize string value as unescaped owned String
, use from_str
instead.
ⓘ
use serde::Deserialize;
#[derive(Deserialize)]
struct Product<'a> {
name: &'a str,
price: u32,
}
// {
// "name": "candy",
// "price": 100
// }
let read = std::fs::File::open("tests/data/product.json").unwrap();
let product: Product = json_with_comments::from_read(&read).unwrap(); // implementation of `Deserialize` is not general enough
assert_eq!(product.name, "candy");
assert_eq!(product.price, 100);