Skip to content

Commit a40e0fc

Browse files
committed
Allow initialising GeoJSON from a dictionary
1 parent f200570 commit a40e0fc

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

Sources/GeoJSONKit/GeoJSON.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,12 @@ public struct GeoJSON: Hashable {
419419
guard let dict = decoded as? [String: Any] else {
420420
throw SerializationError.unexpectedRoot
421421
}
422+
423+
try self.init(from: dict)
424+
}
425+
426+
public init(from dict: [String: Any]) throws {
427+
422428
guard let typeString = dict["type"] as? String, let type = GeoJSONType(rawValue: typeString) else {
423429
throw SerializationError.missingOrInvalidRequiredField("type")
424430
}

Tests/GeoJSONKitTests/GeoJSONParserTest.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ final class GeoJSONParserTest: XCTestCase {
213213
XCTAssertNotNil(parsed)
214214
}
215215

216+
func testFeatureCollectionFromDict() throws {
217+
let data = try XCTestCase.loadData(filename: "featurecollection")
218+
let object = try JSONSerialization.jsonObject(with: data, options: [])
219+
let dict = try XCTUnwrap(object as? [String: Any])
220+
let parsed = try GeoJSON(from: dict)
221+
XCTAssertNotNil(parsed)
222+
}
223+
216224
static var allTests = [
217225
("testPoint", testPoint),
218226
("testMultiPoint", testMultiPoint),
@@ -223,6 +231,7 @@ final class GeoJSONParserTest: XCTestCase {
223231
("testMultiPolygon", testMultiPolygon),
224232
("testFeatureCollection", testFeatureCollection),
225233
("testGeometryCollection", testGeometryCollection),
234+
("testFeatureCollectionFromDict", testFeatureCollectionFromDict),
226235
("testTripGo", testTripGo),
227236
("testWorld", testWorld),
228237
("testNuremberg", testNuremberg),

0 commit comments

Comments
 (0)