Function from_feature

Source
pub fn from_feature<'de, T>(feature: Feature) -> Result<T>
where T: Deserialize<'de>,
Expand description

Interpret a Feature as an instance of type T.

This is analogous to serde_json::from_value

T’s geometry field will be deserialized from feature.geometry. All other fields will be deserialized from feature.properties.

§Examples

use serde::Deserialize;
use geojson::Feature;
use geojson::de::{from_feature, deserialize_geometry, deserialize_single_feature};
use std::str::FromStr;

#[derive(Deserialize)]
struct MyStruct {
    // Deserialize `geometry` as GeoJSON, rather than using the type's default deserialization
    #[serde(deserialize_with = "deserialize_geometry")]
    geometry: geo_types::Point,
    name: String,
}

let geojson_str = r#"{
    "type": "Feature",
    "geometry": { "type": "Point", "coordinates": [1.0, 2.0] },
    "properties": {
        "name": "My Name"
    }
}"#;
let feature  = Feature::from_str(geojson_str).unwrap();

let my_struct: MyStruct = from_feature(feature).unwrap();

assert_eq!("My Name", my_struct.name);
assert_eq!(geo_types::Point::new(1.0, 2.0), my_struct.geometry);

§Errors

Deserialization can fail if T’s implementation of Deserialize decides to fail.