Skip to content
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

Merged
merged 1 commit into from
Nov 11, 2019
Merged

Conversation

avandesa
Copy link
Contributor

@avandesa avandesa commented Nov 8, 2019

There already exists GeoJSON::from_json_object, which converts a serde_json::Map<String, serde_json::Value> to a geojson instance.

The json! 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:

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.

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`.
@urschrei
Copy link
Member

Hi, thanks for the PR! If you'd like to add the Feature etc. variants (no rush though), we can review the PR properly, but this looks good to me.

Copy link
Member

@frewsxcv frewsxcv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@frewsxcv
Copy link
Member

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.

yep yep, sounds good. feel free to open a new pr for those if you end up implementing them

@frewsxcv
Copy link
Member

bors r+

bors bot added a commit that referenced this pull request Nov 11, 2019
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]>
@bors
Copy link
Contributor

bors bot commented Nov 11, 2019

Build succeeded

@bors bors bot merged commit c4a888a into georust:master Nov 11, 2019
@avandesa avandesa deleted the from-json-value branch November 13, 2019 02:58
bors bot added a commit that referenced this pull request Nov 16, 2019
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants