diff --git a/CHANGES.md b/CHANGES.md index 6f437ba..ed713d0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## UNRELEASED + +* Allow parsing Feature/FeatureCollection that are missing a "properties" key. + * + ## 0.22.3 * Added `FromIterator` impl for `FeatureCollection` diff --git a/src/geojson.rs b/src/geojson.rs index ac4c31b..b527d16 100644 --- a/src/geojson.rs +++ b/src/geojson.rs @@ -451,6 +451,31 @@ mod tests { ); } + #[test] + fn test_missing_properties_key() { + let json_value = json!({ + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [102.0, 0.5] + }, + }); + + assert!(json_value.is_object()); + + let geojson: GeoJson = json_value.try_into().unwrap(); + assert_eq!( + geojson, + GeoJson::Feature(Feature { + bbox: None, + geometry: Some(Geometry::new(Value::Point(vec![102.0, 0.5]))), + id: None, + properties: None, + foreign_members: None, + }) + ); + } + #[test] fn test_invalid_json() { let geojson_str = r#"{ diff --git a/src/lib.rs b/src/lib.rs index d44dc45..7783e95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -389,6 +389,10 @@ pub struct Feature { /// Properties /// /// [GeoJSON Format Specification ยง 3.2](https://tools.ietf.org/html/rfc7946#section-3.2) + /// + /// NOTE: This crate will permissively parse a Feature whose json is missing a `properties` key. + /// Because the spec implies that the `properties` key must be present, we will always include + /// the `properties` key when serializing. pub properties: Option, /// Foreign Members /// diff --git a/src/util.rs b/src/util.rs index cc3b606..f9a1b1e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -103,11 +103,12 @@ pub fn get_foreign_members(object: JsonObject) -> Result, Err /// Used by Feature pub fn get_properties(object: &mut JsonObject) -> Result, Error> { - let properties = expect_property(object, "properties")?; + let properties = expect_property(object, "properties"); match properties { - JsonValue::Object(x) => Ok(Some(x)), - JsonValue::Null => Ok(None), - _ => Err(Error::PropertiesExpectedObjectOrNull(properties)), + Ok(JsonValue::Object(x)) => Ok(Some(x)), + Ok(JsonValue::Null) | Err(Error::ExpectedProperty(_)) => Ok(None), + Ok(not_a_dictionary) => Err(Error::PropertiesExpectedObjectOrNull(not_a_dictionary)), + Err(e) => Err(e) } }