Skip to content

Instantly share code, notes, and snippets.

@juliofruta
Created December 3, 2017 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliofruta/5cbe53a2f8128b1df809a983667458ca to your computer and use it in GitHub Desktop.
Save juliofruta/5cbe53a2f8128b1df809a983667458ca to your computer and use it in GitHub Desktop.
JSON 4 Swift bug
// For this JSON:
{
"success": true,
"payload": {
"asks": [{
"book": "btc_mxn",
"price": "5632.24",
"amount": "1.34491802"
},{
"book": "btc_mxn",
"price": "5633.44",
"amount": "0.4259"
},{
"book": "btc_mxn",
"price": "5642.14",
"amount": "1.21642"
}],
"bids": [{
"book": "btc_mxn",
"price": "6123.55",
"amount": "1.12560000"
},{
"book": "btc_mxn",
"price": "6121.55",
"amount": "2.23976"
}],
"updated_at": "2016-04-08T17:52:31.000+00:00",
"sequence": "27214"
}
}
// The generated model on the base should be:
struct Json4Swift_Base : Codable {
let success : Bool?
let payload : Payload?
enum CodingKeys: String, CodingKey {
case success = "success"
case payload = "payload"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
success = try values.decodeIfPresent(Bool.self, forKey: .success)
payload = try values.decodeIfPresent(Payload.self, forKey: .payload)// Payload(from: decoder)
}
}
And not:
struct Json4Swift_Base : Codable {
let success : Bool?
let payload : Payload?
enum CodingKeys: String, CodingKey {
case success = "success"
case payload
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
success = try values.decodeIfPresent(Bool.self, forKey: .success)
payload = try Payload(from: decoder)
}
}
@syedabsar
Copy link

Fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment