mapbox/turf-swift

CLLocationCoordinate2D conformance to Codable should be private

1ec5 opened this issue · 0 comments

1ec5 commented

Turf publicly declares CLLocationCoordinate2D’s conformance to Codable and implements the protocol according to the GeoJSON standard, as an array in longitude, latitude order. But this is problematic if the application or some other library needs to support some other coordinate representation in JSON. For example, Overpass JSON represents coordinate pairs as objects. Turf needs this Codable implementation but doesn’t need to export it publicly. It would be trivial for client code to reimplement this conformance if necessary.

extension CLLocationCoordinate2D: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(longitude)
try container.encode(latitude)
}
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
let longitude = try container.decode(CLLocationDegrees.self)
let latitude = try container.decode(CLLocationDegrees.self)
self.init(latitude: latitude, longitude: longitude)
}
}

/cc @mapbox/navigation-ios @frederoni