Skip to content

Commit b5c19b4

Browse files
committed
feat: generate actual json in `encode function
BREAKING_CHANGE: `encode_geojson` function was generating `dynamic.Dynamic` which is pretty useless from consumer standpoint. Now it is refactored to generate `Json` type which can be stringified with `json.to_string`. Note: currently intentionally left encoding arbitrary `Feature` properties unimplemented. Signed-off-by: Aleksei Gurianov <gurianov@gmail.com>
1 parent 7a022ea commit b5c19b4

13 files changed

Lines changed: 219 additions & 215 deletions

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,27 @@ pub fn main() {
3434
case result {
3535
Ok(geojson) -> {
3636
// Successfully decoded GeoJSON
37-
let encoded = gleojson.encode_geojson(geojson)
38-
// encoded is now a Dynamic representation of the GeoJSON object
39-
// You can use it for further processing or encoding back to JSON
4037
}
4138
Error(errors) -> {
4239
todo
4340
// Handle decoding errors
4441
// errors contains information about what went wrong during decoding
4542
}
4643
}
44+
45+
// Construct GeoJSON from types
46+
let geojson = gleojson.GeoJSONFeatureCollection(
47+
gleojson.FeatureCollection([
48+
gleojson.Feature(
49+
geometry: option.Some(gleojson.Point([1.0, 2.0])),
50+
properties: option.None,
51+
id: option.Some(gleojson.StringId("feature-id")),
52+
),
53+
]),
54+
)
55+
56+
// Encode to JSON string
57+
gleojson.encode_geojson(geojson) |> json.to_string
4758
}
4859
```
4960

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: feature_encode_decode
4+
---
5+
{"id":"feature-id","type":"Feature","geometry":{"type":"Point","coordinates":[1.0,2.0]},"properties":null}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: featurecollection_encode_decode
4+
---
5+
{"type":"FeatureCollection","features":[{"id":"feature-id","type":"Feature","geometry":{"type":"Point","coordinates":[1.0,2.0]},"properties":null}]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: geometrycollection_encode_decode
4+
---
5+
{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[1.0,2.0]},{"type":"LineString","coordinates":[[3.0,4.0],[5.0,6.0]]}]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: linestring_encode_decode
4+
---
5+
{"type":"LineString","coordinates":[[1.0,2.0],[3.0,4.0]]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: multipoint_encode_decode
4+
---
5+
{"type":"MultiPoint","coordinates":[[1.0,2.0],[3.0,4.0]]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: multipolygon_encode_decode
4+
---
5+
{"type":"MultiPolygon","coordinates":[[[[1.0,2.0],[3.0,4.0],[5.0,6.0],[1.0,2.0]]],[[[7.0,8.0],[9.0,10.0],[11.0,12.0],[7.0,8.0]]]]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: point_encode_decode
4+
---
5+
{"type":"Point","coordinates":[1.0,2.0]}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.2.3
3+
title: polygon_encode_decode
4+
---
5+
{"type":"Polygon","coordinates":[[[1.0,2.0],[3.0,4.0],[5.0,6.0],[1.0,2.0]]]}

gleam.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ links = [{ title = "RFC7946", href = "https://datatracker.ietf.org/doc/html/rfc7
88

99
[dependencies]
1010
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
11+
gleam_json = ">= 2.0.0 and < 3.0.0"
1112

1213
[dev-dependencies]
1314
gleeunit = ">= 1.0.0 and < 2.0.0"
14-
gleam_json = ">= 2.0.0 and < 3.0.0"
15+
birdie = ">= 1.2.3 and < 2.0.0"

0 commit comments

Comments
 (0)