Function serde_json::value::from_value [] [src]

pub fn from_value<T>(value: Value) -> Result<T, Error> where
    T: DeserializeOwned

Interpret a serde_json::Value as an instance of type T.

This conversion can fail if the structure of the Value does not match the structure expected by T, for example if T is a struct type but the Value 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.

#[macro_use]
extern crate serde_json;

#[macro_use]
extern crate serde_derive;

extern crate serde;

#[derive(Deserialize, Debug)]
struct User {
    fingerprint: String,
    location: String,
}

fn main() {
    // The type of `j` is `serde_json::Value`
    let j = json!({
                    "fingerprint": "0xF9BA143B95FF6D82",
                    "location": "Menlo Park, CA"
                  });

    let u: User = serde_json::from_value(j).unwrap();
    println!("{:#?}", u);
}