Function rocket::serde::json::from_value

source ·
pub fn from_value<T>(value: Value) -> Result<T, Error>
Available on crate feature json only.
Expand description

Interpret a Value as an instance of type T.

§Example

use rocket::serde::{Deserialize, json};

#[derive(Debug, PartialEq, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Data {
    framework: String ,
    stars: usize,
}

let value = json::json!({
    "framework": "Rocket",
    "stars": 5
});

let data: Data = json::from_value(value).unwrap();
assert_eq!(data, Data { framework: "Rocket".into(), stars: 5, });

§Errors

This conversion can fail if the structure of the input does not match the structure expected by T, for example if T is a struct type but the input contains something other than a JSON map. It can also fail if the structure is correct but T’s implementation of Deserialize decides that something is wrong with the data, for example required struct fields are missing from the JSON map or some number is too big to fit in the expected primitive type.