Hi All,
I am having trouble decoding JSON into any of these structures. Am I missing something?
The JSON format I am currently using to test looks like the following ...
{
"center": {
"x": 129,
"y": 94
},
"radius": 73
}I would assume that the automatic Decodable would understand that center is a CGPoint especially when declared as such in ...
struct Circle {
var center: CGPoint
var radius: CGFloat
... other code here
}
I have tried automatically as well as manually with the standard let values = try decoder.container(keyedBy: CodingKeys.self) etc.
If I set center to CGPoint.zero in the init(from decoder: Decoder) then all works perfectly so it is obviously the way I am formatting the JSON data.
Any help would be most appreciated.
Thank you in advance,
DSC.david
I tried this code in a playground:
var circle = Circle (center: CGPoint (x: 129, y: 94), radius: 73)
let encoder = JSONEncoder ()
let encodedData = try! encoder.encode (circle)
let encodedString = String (bytes: encodedData, encoding: .utf8)!
print (encodedString)
and the output was:
{"center":[129,94],"radius":73}
So it looks like CGPoint has a custom implementation of Codable that represents the coordinates as a vector rather than a struct. I'm not sure what I think about that, since it's not documented anywhere. I did find this, though:
medium.com/swiftly-swift/little-xcode-beta-surprises-core-graphics-codable-conformance-c7ff485c68d2