Skip to content

Commit

Permalink
Fix bounding_box serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kenkoooo authored and adwhit committed Jul 9, 2021
1 parent d5d5e3c commit 8fdca0f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
25 changes: 25 additions & 0 deletions sample_payloads/bounding_box-polygon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"bounding_box": {
"type": "Polygon",
"coordinates": [
[
[
-74.026675,
40.683935
],
[
-74.026675,
40.877483
],
[
-73.910408,
40.877483
],
[
-73.910408,
40.683935
]
]
]
}
}
61 changes: 29 additions & 32 deletions src/place/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ impl fmt::Display for Accuracy {
}

mod serde_bounding_box {
use serde::{Serialize, Deserialize, Serializer, Deserializer};
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub fn deserialize<'de, D>(ser: D) -> Result<Vec<(f64, f64)>, D::Error>
where
Expand All @@ -381,39 +381,36 @@ mod serde_bounding_box {
where
S: Serializer,
{
#[derive(Serialize)]
struct SerBox {
coordinates: Vec<(f64, f64)>,
#[serde(rename = "type")]
box_type: BoxType,
}

#[derive(Serialize)]
enum BoxType {
Polygon,
Point,
}

impl From<&Vec<(f64, f64)>> for SerBox {
fn from(src: &Vec<(f64, f64)>) -> SerBox {
let box_type = if src.len() == 1 {
BoxType::Point
} else {
BoxType::Polygon
};

SerBox {
coordinates: src.clone(),
box_type,
}
}
}

let out: Option<SerBox> = if src.is_empty() {
let value = if src.is_empty() {
None
} else if src.len() == 1 {
Some(serde_json::json!({ "coordinates": src, "type": "Point" }))
} else {
Some(src.into())
Some(serde_json::json!({ "coordinates": [src], "type": "Polygon" }))
};
out.serialize(ser)
value.serialize(ser)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::common::tests::load_file;

#[derive(Debug, Clone, Deserialize, Serialize)]
struct BoundingBox {
#[serde(with = "serde_bounding_box")]
bounding_box: Vec<(f64, f64)>,
}

#[test]
fn parse_and_serialize_polygon_bounding_box() {
let content = load_file("sample_payloads/bounding_box-polygon.json");
let bounding_box = ::serde_json::from_str::<BoundingBox>(&content).unwrap();
assert_eq!(bounding_box.bounding_box.len(), 4);

let raw_value: serde_json::Value = ::serde_json::from_str(&content).unwrap();
let serialized_value = ::serde_json::to_value(&bounding_box).unwrap();
assert_eq!(raw_value, serialized_value);
}
}

0 comments on commit 8fdca0f

Please sign in to comment.