-
Notifications
You must be signed in to change notification settings - Fork 60
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
Add from_json_value
for GeoJson
enum
#119
Conversation
There already exists `GeoJSON::from_json_object`, which converts a `serde_json::Map<String, serde_json::Value>` to a geojson instance. It would be convenient to be able to convert straight from a `serde_json::Value`, as this is what is constructed with the `json!` macro. This commit implements `GeoJson::from_json_value` as a thin wrapper around `from_json_object`.
Hi, thanks for the PR! If you'd like to add the |
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.
thanks!
yep yep, sounds good. feel free to open a new pr for those if you end up implementing them |
bors r+ |
119: Add `from_json_value` for `GeoJson` enum r=frewsxcv a=avandesa There already exists `GeoJSON::from_json_object`, which converts a `serde_json::Map<String, serde_json::Value>` to a geojson instance. The [`json!` macro][macro] from `serde_json` returns a `serde_json::Value`, which while similar to `Map<String, Value>`, is not the same, and there unfortunately is not `impl From<Value> for Map<_, _>`, just the other way around. This PR implements `GeoJson::from_json_value` as a thin wrapper around `from_json_object`, which makes it convenient to use the `json!` macro to quickly create geojson objects from raw JSON in-source, instead of having to use the `parse` methods on string literals. Minimal example use case: ```rust let geojson = GeoJson::from_json_value(json!({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [102.0, 0.5] }, "properties": null, })); ``` If you'd like, I can extend this PR to add similar functions for `Geometry`, `Feature`, and `FeatureCollection`, but I wanted to know if this would be a useful feature first. Edit: Fixed typo in example. [macro]: https://docs.serde.rs/serde_json/macro.json.html Co-authored-by: Alex van de Sandt <[email protected]>
Build succeeded |
120: Add `TryFrom` impls for JsonObject and JsonValue r=frewsxcv a=avandesa For each GeoJson type, an `impl TryFrom<JsonObject>` and `impl TryFrom<JsonValue>` is added, and the analagous `from_json_object` and `from_json_value` functions are converted to use the new traits. This makes it possible to simplify the instantiation of GeoJson types like this: ```rust let feature: Feature = json!({ "type": "Feature", "geometry": { "type": "Point", "coordinates": [102.0, 0.5] }, "properties": null, }).try_into().unwrap(); ``` impls are added for: * `GeoJson` * `Geometry` * `Feature`, and * `FeatureCollection`. API stability is maintained - the only change is that the `from_json_object` methods take `JsonObject` instead of `mut JsonObject`, but this should not break anything. Tests are added where appropriate. This commit expands on #119. Co-authored-by: Alex van de Sandt <[email protected]>
There already exists
GeoJSON::from_json_object
, which converts aserde_json::Map<String, serde_json::Value>
to a geojson instance.The
json!
macro fromserde_json
returns aserde_json::Value
, which while similar toMap<String, Value>
, is not the same, and there unfortunately is notimpl From<Value> for Map<_, _>
, just the other way around.This PR implements
GeoJson::from_json_value
as a thin wrapper aroundfrom_json_object
, which makes it convenient to use thejson!
macro to quickly create geojson objects from raw JSON in-source, instead of having to use theparse
methods on string literals.Minimal example use case:
If you'd like, I can extend this PR to add similar functions for
Geometry
,Feature
, andFeatureCollection
, but I wanted to know if this would be a useful feature first.Edit: Fixed typo in example.