Function geojson::de::deserialize_geometry

source ·
pub fn deserialize_geometry<'de, D, G>(deserializer: D) -> Result<G, D::Error>
where D: Deserializer<'de>, G: TryFrom<Geometry>, G::Error: Display,
Expand description

serde::deserialize_with helper to deserialize a GeoJSON Geometry into another type, like a geo_types Geometry.

§Examples

use serde::Deserialize;
use geojson::de::deserialize_geometry;

#[derive(Deserialize)]
struct MyStruct {
    #[serde(deserialize_with = "deserialize_geometry")]
    geometry: geo_types::Point<f64>,
    name: String,
}

let feature_collection_str = r#"{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": { "type": "Point", "coordinates": [11.1, 22.2] },
            "properties": { "name": "Downtown" }
        },
        {
            "type": "Feature",
            "geometry": { "type": "Point", "coordinates": [33.3, 44.4] },
            "properties": { "name": "Uptown" }
        }
    ]
}"#;

let features: Vec<MyStruct> = geojson::de::deserialize_feature_collection_str_to_vec(feature_collection_str).unwrap();

assert_eq!(features[0].name, "Downtown");
assert_eq!(features[0].geometry.x(), 11.1);