Function json_with_comments::de::from_path
source · pub fn from_path<D>(p: &Path) -> Result<D>where
D: DeserializeOwned,
Expand description
Deserialize a JSON with comments text of the given path as type D
.
§Examples
use serde::Deserialize;
#[derive(Deserialize)]
struct Product {
name: String,
price: u32,
}
// {
// "name": "candy",
// "price": 100
// }
let path = std::path::Path::new("tests/data/product.json");
let product: Product = json_with_comments::from_path(path).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, same as from_file
.
ⓘ
use serde::Deserialize;
#[derive(Deserialize)]
struct Product<'a> {
name: &'a str,
price: u32,
}
// {
// "name": "candy",
// "price": 100
// }
let path = std::path::Path::new("tests/data/product.json");
let product: Product = json_with_comments::from_path(path).unwrap(); // implementation of `Deserialize` is not general enough
assert_eq!(product.name, "candy");
assert_eq!(product.price, 100);