16
16
17
17
import gleam/dict
18
18
import gleam/dynamic
19
- import gleam/list
19
+ import gleam/json
20
20
import gleam/option
21
21
import gleam/result
22
22
@@ -64,90 +64,112 @@ pub type GeoJSON {
64
64
65
65
// Encoding Functions
66
66
67
- /// Encodes a geometry into a dynamic value .
68
- fn encode_geometry ( geometry : Geometry ) -> dynamic . Dynamic {
67
+ /// Encodes a geometry into a JSON object .
68
+ fn encode_geometry ( geometry : Geometry ) -> json . Json {
69
69
case geometry {
70
70
Point ( coordinates ) -> {
71
- dict . from_list ( [
72
- # ( "type" , dynamic . from ( "Point" ) ) ,
73
- # ( "coordinates" , dynamic . from ( coordinates ) ) ,
71
+ json . object ( [
72
+ # ( "type" , json . string ( "Point" ) ) ,
73
+ # ( "coordinates" , json . array ( coordinates , of : json . float ) ) ,
74
74
] )
75
75
}
76
76
MultiPoint ( multipoint ) -> {
77
- dict . from_list ( [
78
- # ( "type" , dynamic . from ( "MultiPoint" ) ) ,
79
- # ( "coordinates" , dynamic . from ( multipoint ) ) ,
77
+ json . object ( [
78
+ # ( "type" , json . string ( "MultiPoint" ) ) ,
79
+ # (
80
+ "coordinates" ,
81
+ json . array ( multipoint , of : json . array ( _, of : json . float ) ) ,
82
+ ) ,
80
83
] )
81
84
}
82
85
LineString ( linestring ) -> {
83
- dict . from_list ( [
84
- # ( "type" , dynamic . from ( "LineString" ) ) ,
85
- # ( "coordinates" , dynamic . from ( linestring ) ) ,
86
+ json . object ( [
87
+ # ( "type" , json . string ( "LineString" ) ) ,
88
+ # (
89
+ "coordinates" ,
90
+ json . array ( linestring , of : json . array ( _, of : json . float ) ) ,
91
+ ) ,
86
92
] )
87
93
}
88
94
MultiLineString ( multilinestring ) -> {
89
- dict . from_list ( [
90
- # ( "type" , dynamic . from ( "MultiLineString" ) ) ,
91
- # ( "coordinates" , dynamic . from ( multilinestring ) ) ,
95
+ json . object ( [
96
+ # ( "type" , json . string ( "MultiLineString" ) ) ,
97
+ # (
98
+ "coordinates" ,
99
+ json . array ( multilinestring , of : json . array ( _, of : json . array (
100
+ _,
101
+ of : json . float ,
102
+ ) ) ) ,
103
+ ) ,
92
104
] )
93
105
}
94
106
Polygon ( polygon ) -> {
95
- dict . from_list ( [
96
- # ( "type" , dynamic . from ( "Polygon" ) ) ,
97
- # ( "coordinates" , dynamic . from ( polygon ) ) ,
107
+ json . object ( [
108
+ # ( "type" , json . string ( "Polygon" ) ) ,
109
+ # (
110
+ "coordinates" ,
111
+ json . array ( polygon , of : json . array ( _, of : json . array (
112
+ _,
113
+ of : json . float ,
114
+ ) ) ) ,
115
+ ) ,
98
116
] )
99
117
}
100
118
MultiPolygon ( multipolygon ) -> {
101
- dict . from_list ( [
102
- # ( "type" , dynamic . from ( "MultiPolygon" ) ) ,
103
- # ( "coordinates" , dynamic . from ( multipolygon ) ) ,
119
+ json . object ( [
120
+ # ( "type" , json . string ( "MultiPolygon" ) ) ,
121
+ # (
122
+ "coordinates" ,
123
+ json . array (
124
+ multipolygon ,
125
+ of : json . array ( _, of : json . array ( _, of : json . array (
126
+ _,
127
+ of : json . float ,
128
+ ) ) ) ,
129
+ ) ,
130
+ ) ,
104
131
] )
105
132
}
106
133
GeometryCollection ( collection ) -> {
107
- let geometries_dyn_list = list . map ( collection , encode_geometry )
108
- dict . from_list ( [
109
- # ( "type" , dynamic . from ( "GeometryCollection" ) ) ,
110
- # ( "geometries" , dynamic . from ( geometries_dyn_list ) ) ,
134
+ json . object ( [
135
+ # ( "type" , json . string ( "GeometryCollection" ) ) ,
136
+ # ( "geometries" , json . array ( collection , of : encode_geometry ) ) ,
111
137
] )
112
138
}
113
139
}
114
- |> dynamic . from
115
140
}
116
141
117
- /// Encodes a feature into a dynamic value .
118
- fn encode_feature ( feature : Feature ) -> dynamic . Dynamic {
119
- let Feature ( geometry_opt , properties_opt , id_opt ) = feature
120
- let geometry_dyn = case geometry_opt {
142
+ /// Encodes a feature into a JSON object .
143
+ fn encode_feature ( feature : Feature ) -> json . Json {
144
+ let Feature ( geometry_opt , _properties_opt , id_opt ) = feature
145
+ let geometry_json = case geometry_opt {
121
146
option . Some ( geometry ) -> encode_geometry ( geometry )
122
- option . None -> dynamic . from ( Nil )
123
- }
124
- let properties_dyn = case properties_opt {
125
- option . Some ( props ) -> dynamic . from ( props )
126
- option . None -> dynamic . from ( Nil )
147
+ option . None -> json . null ( )
127
148
}
128
- let base_obj =
129
- dict . from_list ( [
130
- # ( "type" , dynamic . from ( "Feature" ) ) ,
131
- # ( "geometry" , geometry_dyn ) ,
132
- # ( "properties" , properties_dyn ) ,
133
- ] )
149
+ // let properties_json = case properties_opt {
150
+ // option.Some(props) -> json.object(props)
151
+ // option.None -> json.object([])
152
+ // }
153
+ let base_obj = [
154
+ # ( "type" , json . string ( "Feature" ) ) ,
155
+ # ( "geometry" , geometry_json ) ,
156
+ # ( "properties" , json . null ( ) ) ,
157
+ ]
134
158
case id_opt {
135
- option . Some ( StringId ( id ) ) -> dict . insert ( base_obj , "id" , dynamic . from ( id ) )
136
- option . Some ( NumberId ( id ) ) -> dict . insert ( base_obj , "id" , dynamic . from ( id ) )
159
+ option . Some ( StringId ( id ) ) -> [ # ( "id" , json . string ( id ) ) , .. base_obj ]
160
+ option . Some ( NumberId ( id ) ) -> [ # ( "id" , json . float ( id ) ) , .. base_obj ]
137
161
option . None -> base_obj
138
162
}
139
- |> dynamic . from
163
+ |> json . object
140
164
}
141
165
142
- /// Encodes a feature collection into a dynamic value .
143
- fn encode_featurecollection ( collection : FeatureCollection ) -> dynamic . Dynamic {
166
+ /// Encodes a feature collection into a JSON object .
167
+ fn encode_featurecollection ( collection : FeatureCollection ) -> json . Json {
144
168
let FeatureCollection ( features ) = collection
145
- let features_dyn_list = list . map ( features , encode_feature )
146
- dict . from_list ( [
147
- # ( "type" , dynamic . from ( "FeatureCollection" ) ) ,
148
- # ( "features" , dynamic . from ( features_dyn_list ) ) ,
169
+ json . object ( [
170
+ # ( "type" , json . string ( "FeatureCollection" ) ) ,
171
+ # ( "features" , json . array ( features , of : encode_feature ) ) ,
149
172
] )
150
- |> dynamic . from
151
173
}
152
174
153
175
/// Encodes a GeoJSON object into a dynamic value.
@@ -159,7 +181,7 @@ fn encode_featurecollection(collection: FeatureCollection) -> dynamic.Dynamic {
159
181
/// let encoded = encode_geojson(point)
160
182
/// // encoded will be a dynamic representation of the GeoJSON object
161
183
/// ```
162
- pub fn encode_geojson ( geojson : GeoJSON ) -> dynamic . Dynamic {
184
+ pub fn encode_geojson ( geojson : GeoJSON ) -> json . Json {
163
185
case geojson {
164
186
GeoJSONGeometry ( geometry ) -> encode_geometry ( geometry )
165
187
GeoJSONFeature ( feature ) -> encode_feature ( feature )
0 commit comments