When parsing valid GeoJSON data containing escaped double-quotes, the MKGeoJSONDecoder fails with error.
Steps to reproduce:
Run the following code block in a .swift playground
import Foundation
import MapKit
//Valid data verifed using http://geojson.io
if let data = """
{
"features": [
{
"geometry": {
"coordinates": [
13.38678,
52.52548
],
"type": "Point"
},
"properties": {
"description": " \" text in double quotes \" ",
"href": "/sen/web/service/liefer-und-abholdienste/index.php/detail/1848",
"id": "/sen/web/service/liefer-und-abholdienste/index.php/detail/1848",
"title": "1848"
},
"type": "Feature"
}
],
"type": "FeatureCollection"
}
""".data(using: .utf8) {
let decoder = MKGeoJSONDecoder()
do {
let decoded = try decoder.decode(data)
if let firstFeature = decoded.first as? MKGeoJSONFeature,
let firstCoordinate = firstFeature.geometry.first?.coordinate {
print("Latitude: \(firstCoordinate.latitude) Longitude : \(firstCoordinate.longitude)")
}
} catch (let error) {
print(error)
}
}
The catch block will report an error saying: “The data couldn’t be read because it isn’t in the correct format.”
If we change the “description” string and remove the escaped double-quotes “\
, the MKGeoJSONDecoder decodes the data without issue.
Edit: After removing localisedDescription, the error shown is:
Error Domain=NSCocoaErrorDomain Code=3840
"Badly formed object around character 325."
UserInfo={NSDebugDescription=Badly formed object around character 325.}