-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Serialize
without going through JsonObject
#233
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,7 @@ use std::str::FromStr; | |
use crate::errors::{Error, Result}; | ||
use crate::{util, Feature, Geometry, Value}; | ||
use crate::{JsonObject, JsonValue}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use serde_json::json; | ||
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
impl From<Geometry> for Feature { | ||
fn from(geom: Geometry) -> Feature { | ||
|
@@ -55,35 +54,13 @@ impl FromStr for Feature { | |
|
||
impl<'a> From<&'a Feature> for JsonObject { | ||
fn from(feature: &'a Feature) -> JsonObject { | ||
let mut map = JsonObject::new(); | ||
map.insert(String::from("type"), json!("Feature")); | ||
map.insert( | ||
String::from("geometry"), | ||
serde_json::to_value(&feature.geometry).unwrap(), | ||
); | ||
if let Some(ref properties) = feature.properties { | ||
map.insert( | ||
String::from("properties"), | ||
serde_json::to_value(properties).unwrap(), | ||
); | ||
} else { | ||
map.insert( | ||
String::from("properties"), | ||
serde_json::to_value(Some(serde_json::Map::new())).unwrap(), | ||
); | ||
} | ||
if let Some(ref bbox) = feature.bbox { | ||
map.insert(String::from("bbox"), serde_json::to_value(bbox).unwrap()); | ||
} | ||
if let Some(ref id) = feature.id { | ||
map.insert(String::from("id"), serde_json::to_value(id).unwrap()); | ||
match serde_json::to_value(feature).unwrap() { | ||
serde_json::Value::Object(obj) => obj, | ||
value => panic!( | ||
"serializing Feature should result in an Object, but got something {:?}", | ||
value | ||
), | ||
} | ||
if let Some(ref foreign_members) = feature.foreign_members { | ||
for (key, value) in foreign_members { | ||
map.insert(key.to_owned(), value.to_owned()); | ||
} | ||
} | ||
map | ||
} | ||
} | ||
|
||
|
@@ -182,7 +159,26 @@ impl Serialize for Feature { | |
where | ||
S: Serializer, | ||
{ | ||
JsonObject::from(self).serialize(serializer) | ||
let mut map = serializer.serialize_map(None)?; | ||
map.serialize_entry("type", "Feature")?; | ||
map.serialize_entry("geometry", &self.geometry)?; | ||
if let Some(ref properties) = self.properties { | ||
map.serialize_entry("properties", properties)?; | ||
} else { | ||
map.serialize_entry("properties", &JsonObject::new())?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be null instead of an empty object. I think this reflects a gap in our test coverage. Can you add it if you agree? Something like: #[test]
fn feature_json_null_properties() {
let geojson_str = r#"{
"geometry": null,
"properties": null,
"type":"Feature"
}"#;
let geojson = geojson_str.parse::<GeoJson>().unwrap();
let GeoJson::Feature(feature) = geojson else {
panic!("unexpected geojson");
};
assert!(feature.properties.is_none());
assert_eq!(feature.to_string(), r#"{"type":"Feature","geometry":null,"properties":null}"#);
}
edit: updated the quote. I initially copied the wrong doc snippet. 🤦 (from https://datatracker.ietf.org/doc/html/rfc7946#section-3.2) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Changed the behavior and added a test. |
||
} | ||
if let Some(ref bbox) = self.bbox { | ||
map.serialize_entry("bbox", bbox)?; | ||
} | ||
if let Some(ref id) = self.id { | ||
map.serialize_entry("id", id)?; | ||
} | ||
if let Some(ref foreign_members) = self.foreign_members { | ||
for (key, value) in foreign_members { | ||
map.serialize_entry(key, value)?; | ||
} | ||
} | ||
map.end() | ||
} | ||
} | ||
|
||
|
@@ -229,8 +225,7 @@ mod tests { | |
use std::str::FromStr; | ||
|
||
fn feature_json_str() -> &'static str { | ||
"{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"properties\":{},\"type\":\ | ||
\"Feature\"}" | ||
"{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.1,2.1]},\"properties\":{}}" | ||
} | ||
|
||
fn properties() -> Option<JsonObject> { | ||
|
@@ -314,8 +309,7 @@ mod tests { | |
#[test] | ||
fn test_display_feature() { | ||
let f = feature().to_string(); | ||
assert_eq!(f, "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"properties\":{},\"type\":\ | ||
\"Feature\"}"); | ||
assert_eq!(f, "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.1,2.1]},\"properties\":{}}"); | ||
} | ||
|
||
#[test] | ||
|
@@ -344,7 +338,7 @@ mod tests { | |
|
||
#[test] | ||
fn encode_decode_feature_with_id_number() { | ||
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":0,\"properties\":{},\"type\":\"Feature\"}"; | ||
let feature_json_str = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.1,2.1]},\"properties\":{},\"id\":0}"; | ||
let feature = crate::Feature { | ||
geometry: Some(Geometry { | ||
value: Value::Point(vec![1.1, 2.1]), | ||
|
@@ -370,7 +364,7 @@ mod tests { | |
|
||
#[test] | ||
fn encode_decode_feature_with_id_string() { | ||
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":\"foo\",\"properties\":{},\"type\":\"Feature\"}"; | ||
let feature_json_str = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.1,2.1]},\"properties\":{},\"id\":\"foo\"}"; | ||
let feature = crate::Feature { | ||
geometry: Some(Geometry { | ||
value: Value::Point(vec![1.1, 2.1]), | ||
|
@@ -416,7 +410,8 @@ mod tests { | |
fn encode_decode_feature_with_foreign_member() { | ||
use crate::JsonObject; | ||
use serde_json; | ||
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"other_member\":\"some_value\",\"properties\":{},\"type\":\"Feature\"}"; | ||
let feature_json_str = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.1,2.1]},\"properties\":{},\"other_member\":\"some_value\"}"; | ||
|
||
let mut foreign_members = JsonObject::new(); | ||
foreign_members.insert( | ||
String::from("other_member"), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to suggest passing in the length here, but
serde_json
doesn't seem to use it.