is there any way to tell JSONDecoder to convert incoming decimals to Strings?
public struct Transaction: Decodable
{
public let total: NSDecimalNumber?
enum CodingKeys: String, CodingKey {
case total = "AMOUNT"
}
public init(from decoder: Decoder) throws
{
let values = try decoder.container(keyedBy: CodingKeys.self)
let total = try values.decode(Decimal.self, forKey: .total)
self.total = NSDecimalNumber(decimal: total);
}
}
Consider what would happen when AMOUNT is something like 397289527598234759823475455622342424358363514.42
I suppose with the code that I have I'd get nonConformingFloatDecodingStrategy exception with no recourse to recover from that or loss of precision.
Struggles around idiotic swift Decimal are documented all over the s.o. in particular here: