Skip to content

Instantly share code, notes, and snippets.

@kylebshr
Last active May 29, 2020 06:40
Show Gist options
  • Save kylebshr/24421a8d12bbb78d07c0b572d9df2a1d to your computer and use it in GitHub Desktop.
Save kylebshr/24421a8d12bbb78d07c0b572d9df2a1d to your computer and use it in GitHub Desktop.
Can't figure out why this doesn't decode??
import Foundation
struct PlaidBalance: Codable {
var current: Decimal
var available: Decimal
var isoCurrencyCode: String
}
struct PlaidAccount: Codable {
enum AccountType: String, Codable {
case depository
}
enum AccountSubtype: String, Codable {
case checking
case savings
}
var accountID: String
var balances: PlaidBalance
var name: String
var type: AccountType
var subtype: AccountSubtype
}
struct PlaidAccounts: Codable {
var accounts: [PlaidAccount]
}
let string = """
{
"accounts": [
{
"account_id": "qLlzRl7eeLS4b4BND165U79yl7X7vjcdl9rEW",
"balances": {
"current": 110,
"available": 100,
"iso_currency_code": "USD"
},
"name": "Plaid Checking",
"type": "depository",
"subtype": "checking"
},
{
"account_id": "K1RxnRMBB1hLkLX1b4Wgtngvrn4nlGcVoepyq",
"balances": {
"current": 210,
"available": 200,
"iso_currency_code": "USD"
},
"name": "Plaid Saving",
"type": "depository",
"subtype": "savings"
}
]
}
"""
let data = string.data(using: .utf8)!
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
print(try decoder.decode(PlaidAccounts.self, from: data))
} catch {
print(error)
}
/*
keyNotFound(CodingKeys(stringValue: "accountID", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "accounts", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"accountID\", intValue: nil) (\"accountID\"), with divergent representation accountId, converted to account_id.", underlyingError: nil))
*/
@lugearma
Copy link

looks like a bug, replacing accountID for accountId works, basically it fails when you put more than one capital letter next to other one.

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