@@ -110,18 +110,18 @@ public struct GeoJSON: Hashable {
110110 }
111111 }
112112
113- fileprivate func toJSON( ) -> [ String : Any ] {
113+ fileprivate func toJSON( prune : Bool ) -> [ String : Any ] {
114114 var json : [ String : Any ] = [
115115 " type " : type. rawValue
116116 ]
117117
118118 switch self {
119119 case . single( let geometry) :
120- json [ " coordinates " ] = geometry. coordinatesJSON ( )
120+ json [ " coordinates " ] = geometry. coordinatesJSON ( prune : prune )
121121 case . multi( let geometries) :
122- json [ " coordinates " ] = geometries. map { $0. coordinatesJSON ( ) }
122+ json [ " coordinates " ] = geometries. map { $0. coordinatesJSON ( prune : prune ) }
123123 case . collection( let geometries) :
124- json [ " geometries " ] = geometries. map { $0. toJSON ( ) }
124+ json [ " geometries " ] = geometries. map { $0. toJSON ( prune : prune ) }
125125 }
126126
127127 return json
@@ -262,14 +262,25 @@ public struct GeoJSON: Hashable {
262262 }
263263 }
264264
265- func coordinatesJSON( ) -> [ Any ] {
266- switch self {
267- case . point( let position) :
268- return position. toJSON ( ) . prune
269- case . lineString( let lineString) :
270- return lineString. positions. map { $0. toJSON ( ) . prune }
271- case . polygon( let polygon) :
272- return polygon. positionsArray. map { $0. map { $0. toJSON ( ) . prune } }
265+ func coordinatesJSON( prune: Bool ) -> [ Any ] {
266+ if prune {
267+ switch self {
268+ case . point( let position) :
269+ return position. toJSON ( ) . prune
270+ case . lineString( let lineString) :
271+ return lineString. positions. map { $0. toJSON ( ) . prune }
272+ case . polygon( let polygon) :
273+ return polygon. positionsArray. map { $0. map { $0. toJSON ( ) . prune } }
274+ }
275+ } else {
276+ switch self {
277+ case . point( let position) :
278+ return position. toJSON ( )
279+ case . lineString( let lineString) :
280+ return lineString. positions. map { $0. toJSON ( ) }
281+ case . polygon( let polygon) :
282+ return polygon. positionsArray. map { $0. map { $0. toJSON ( ) } }
283+ }
273284 }
274285 }
275286 }
@@ -296,12 +307,12 @@ public struct GeoJSON: Hashable {
296307 id = dict [ " id " ] as? AnyHashable
297308 }
298309
299- public func toJSON( ) -> [ String : Any ] {
310+ public func toJSON( prune : Bool = true ) -> [ String : Any ] {
300311 var json : [ String : Any ] = [
301312 " type " : " Feature " ,
302- " geometry " : geometry. toJSON ( )
313+ " geometry " : geometry. toJSON ( prune : prune )
303314 ]
304- json [ " properties " ] = properties? . prune
315+ json [ " properties " ] = prune ? properties? . prune : properties
305316 json [ " id " ] = id
306317 return json
307318 }
@@ -471,28 +482,31 @@ public struct GeoJSON: Hashable {
471482 }
472483
473484
474- public func toData( options: JSONSerialization . WritingOptions = [ ] ) throws -> Data {
475- return try JSONSerialization . data ( withJSONObject: toJSON ( ) , options: options)
485+ public func toData( options: JSONSerialization . WritingOptions = [ ] , prune : Bool = true ) throws -> Data {
486+ return try JSONSerialization . data ( withJSONObject: toJSON ( prune : prune ) , options: options)
476487 }
477488
478- public func toJSON( ) -> [ String : Any ] {
489+ public func toJSON( prune : Bool = true ) -> [ String : Any ] {
479490 var json = [ String: Any] ( )
480491
481492 json [ " type " ] = type. rawValue
482- json [ " bbox " ] = boundingBox? . toJSON ( ) . prune
493+
494+ let bbJson = boundingBox? . toJSON ( )
495+ json [ " bbox " ] = prune ? bbJson? . prune : bbJson
483496
484497 let objectJson : [ String : Any ]
485498 switch object {
486499 case . feature( let feature) :
487- objectJson = feature. toJSON ( )
500+ objectJson = feature. toJSON ( prune : prune )
488501 case . featureCollection( let features) :
489- objectJson = [ " features " : features. map { $0. toJSON ( ) } ]
502+ objectJson = [ " features " : features. map { $0. toJSON ( prune : prune ) } ]
490503 case . geometry( let geometry) :
491- objectJson = geometry. toJSON ( )
504+ objectJson = geometry. toJSON ( prune : prune )
492505 }
493506 json. merge ( objectJson) { a, _ in a }
494507
495- json. merge ( additionalFields. prune) { a, _ in a }
508+ let additional = prune ? additionalFields. prune : additionalFields
509+ json. merge ( additional) { a, _ in a }
496510
497511 return json
498512 }
0 commit comments