pub fn to_value<T>(item: T) -> Result<Value, Error>where
T: Serialize,
Available on crate feature
json
only.Expand description
Convert a T
into a Value
, an opaque value representing JSON data.
§Example
use rocket::serde::{Deserialize, Serialize, json};
#[derive(Deserialize, Serialize)]
#[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.clone()).unwrap();
let data_value = json::to_value(data).unwrap();
assert_eq!(value, data_value);
§Errors
This conversion fails if T
’s implementation of Serialize
decides to fail
or if T
contains a map with non-string keys.