Skip to content

Commit

Permalink
Added convenience wrappers for stringifying geojson (#229)
Browse files Browse the repository at this point in the history
Co-authored-by: Jere Suikkila <[email protected]>
  • Loading branch information
michaelkirk and jeresuikkila authored Oct 19, 2024
1 parent bedb195 commit ab5d70d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Added `GeometryCollection::try_from(&GeoJson)` and deprecated
`quick_collection` for conventional naming and simpler docs.
* <https://github.com/georust/geojson/pulls/214>
* Added `GeoJson::to_string_pretty` as convenience wrappers around the same `serde_json` methods.

## 0.24.1

Expand Down
18 changes: 18 additions & 0 deletions examples/geojson_to_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::error::Error;
use std::fs::File;
use std::io::BufReader;

use geojson::{Feature, GeoJson};

fn main() -> Result<(), Box<dyn Error>> {
let file_reader = BufReader::new(File::open("tests/fixtures/canonical/good-feature.geojson")?);

let feature: Feature = serde_json::from_reader(file_reader)?;

let geojson: GeoJson = feature.into();

println!("{}", &geojson.to_string());
println!("{}", &geojson.to_string_pretty()?);

Ok(())
}
7 changes: 7 additions & 0 deletions src/geojson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ impl GeoJson {
{
serde_json::from_reader(rdr)
}

/// Convenience wrapper for [serde_json::to_string_pretty()]
pub fn to_string_pretty(self) -> Result<String> {
::serde_json::to_string_pretty(&self)
.map_err(|err| Error::MalformedJson(err))
.and_then(|s| Ok(s.to_string()))
}
}

impl TryFrom<JsonObject> for GeoJson {
Expand Down

0 comments on commit ab5d70d

Please sign in to comment.