Function geojson::de::deserialize_single_feature

source ·
pub fn deserialize_single_feature<'de, T>(
    feature_reader: impl Read,
) -> Result<T>
where T: Deserialize<'de>,
Expand description

Deserialize a single GeoJSON Feature into your custom struct.

It’s more common to deserialize a FeatureCollection than a single feature. If you’re looking to do that, see deserialize_feature_collection instead.

Your struct must implement or derive serde::Deserialize.

§Examples

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

#[derive(Deserialize)]
struct MyStruct {
    // You must use the `deserialize_geometry` helper if you are using geo_types or some other
    // geometry representation other than geojson::Geometry
    #[serde(deserialize_with = "deserialize_geometry")]
    geometry: geo_types::Point<f64>,
    name: String,
}

let feature_str = r#"{
    "type": "Feature",
    "geometry": { "type": "Point", "coordinates": [11.1, 22.2] },
    "properties": { "name": "Downtown" }
}"#;
let reader = feature_str.as_bytes();

// build your struct from GeoJSON
let my_struct = geojson::de::deserialize_single_feature::<MyStruct>(reader).expect("valid geojson for MyStruct");

assert_eq!(my_struct.name, "Downtown");
assert_eq!(my_struct.geometry.x(), 11.1);