Function geojson::ser::serialize_geometry

source ·
pub fn serialize_geometry<IG, S>(
    geometry: IG,
    ser: S,
) -> Result<S::Ok, S::Error>
where IG: TryInto<Geometry>, S: Serializer,
Expand description

serde::serialize_with helper to serialize a type like a geo_types, as a GeoJSON Geometry.

§Examples

use serde::Serialize;
use geojson::ser::serialize_geometry;

#[derive(Serialize)]
struct MyStruct {
    // Serialize as geojson, rather than using the type's default serialization
    #[serde(serialize_with = "serialize_geometry")]
    geometry: geo_types::Point<f64>,
    name: String,
}

let my_structs = vec![
    MyStruct {
        geometry: geo_types::Point::new(11.1, 22.2),
        name: "Downtown".to_string()
    },
    MyStruct {
        geometry: geo_types::Point::new(33.3, 44.4),
        name: "Uptown".to_string()
    }
];

let geojson_string = geojson::ser::to_feature_collection_string(&my_structs).unwrap();

assert!(geojson_string.contains(r#""geometry":{"coordinates":[11.1,22.2],"type":"Point"}"#));