Skip to content

Commit e868a8b

Browse files
authored
Fix model serialization on Linux (#12)
* Fix model serialization on Linux * Some more comments, while we're at it
1 parent 8205368 commit e868a8b

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

Sources/GeoJSONKit/GeoJSON+Codable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ extension GeoJSON.Feature {
118118
var encoder = JSONEncoder()
119119
configure(&encoder)
120120
let data = try encoder.encode(model)
121-
let asDict = try JSONSerialization.jsonObject(with: data) as? [String: AnyHashable]
121+
let decoded = try JSONSerialization.jsonObject(with: data)
122+
123+
// This is needed for Linux as JSONSerialization behaves differently there
124+
let asDict = (decoded as? [String: Any])?.compactMapValues(Adjuster.hashable(_:))
125+
122126
self.init(geometry: geometry, properties: asDict, id: id)
123127
}
124128

Sources/GeoJSONKit/GeoJSON.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ public struct GeoJSON: Hashable {
296296
/// Features in GeoJSON contain a Geometry object and additional properties.
297297
public struct Feature: Hashable {
298298
public var geometry: GeometryObject
299+
300+
/// Optional properties, as a dictionary.
299301
public var properties: [String: AnyHashable]?
302+
300303
public var id: AnyHashable? // number or string
301304

302305
public init(geometry: GeometryObject, properties: [String: AnyHashable]? = nil, id: AnyHashable? = nil) {
@@ -315,6 +318,9 @@ public struct GeoJSON: Hashable {
315318
id = dict["id"] as? AnyHashable
316319
}
317320

321+
/// - Warning: If there are no properties, the `properties` field will be omitted, to avoid dealing
322+
/// with `nil` values in a dictionary. This is strictly speaking not compliant with the [GeoJSON spec](https://datatracker.ietf.org/doc/html/rfc7946#section-3.2),
323+
/// which says this value should be present with a JSON null value.
318324
public func toJSON(prune: Bool = true) -> [String: AnyHashable] {
319325
var json: [String: Any] = [
320326
"type": "Feature",
@@ -539,7 +545,7 @@ fileprivate extension Array where Element == GeoJSON.Degrees {
539545
}
540546
}
541547

542-
fileprivate enum Adjuster {
548+
enum Adjuster {
543549
static func prune(_ value: Any) -> Any {
544550
if let dict = value as? [String: Any] {
545551
return dict.mapValues(prune(_:))

Tests/GeoJSONKitTests/GeoJSONCodableTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ final class GeoJSONCodableTest: XCTestCase {
206206
let model = Model(name: "Test", number: 3517, date: Date())
207207
let feature = try GeoJSON.Feature(geometry: point, model: model)
208208
XCTAssertNotNil(feature)
209+
XCTAssertNotNil(feature.properties)
209210

210211
let restored = try feature.model(as: Model.self)
211212
XCTAssertEqual(model, restored)

0 commit comments

Comments
 (0)