Created
December 3, 2017 15:09
-
-
Save juliofruta/5cbe53a2f8128b1df809a983667458ca to your computer and use it in GitHub Desktop.
JSON 4 Swift bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed