Parsing Decimal values from JSON

With regard to your initial question about JSON. You're correct that, when you are working with currency, you really must use decimal types. However, the JSON standard does not guarantee any particular way of parsing numbers or any precision. Moreover, there is no guarantee that the value you see in a JSON file wasn't serialized from a Double to begin with.

So if you have no control over the data from the external service, this is not a solvable problem here because it's not a problem with how Swift parses JSON. It is entirely acceptable to the JSON standard to conclude that, in general, any floating-point value in a JSON file is more likely to be a serialized Double than a serialized decimal value. If you did have control over the data, serializing as a string is the most reliable way to ensure the right precision when you parse JSON.


As to @skagedal's point, I'll refer you to an earlier thread about conversion from Double to Decimal:

As I wrote there, the behavior you see is understandably surprising but actually what the semantics of ExpressibleByFloatLiteral demand. Technically, 3.133 is a literal that represents a binary floating-point value, rounded to some precision. There is no literal in Swift for a decimal value. To get there from here is nontrivial, but it's not the primary issue with JSON that was the original question.