[swift-users] Parsing Decimal values from JSON
evan.ireland at sap.com
Tue Oct 31 16:13:33 CDT 2017
At json.org the grammar for numbers does not limit the precision to IEEE double precision.
There are specifications (e.g. OData 4.0) which pemit decimal/integer values being encoded as JSON numbers that might go outside the range of IEEE double, with an expectation that agents (clients and servers) will preserve the precision.
Therefore you may find that when working with such specifications, and with JSON parsing in Swift, you need your own (or a third-party) JSON parser if arbitrary integer/decimal precision needs to be retained when using JSON numbers rather than JSON strings for the arbitrary precision integer/decimal encoding.
Some folks might wish to consider this as a defect in the JSON parser, others may see it as an issue with the way that values are being encoded in JSON.
Now I-JSON (see https://datatracker.ietf.org/doc/draft-ietf-json-i-json/03/?include_text=1) defines some extra constraints to avoid such interoperability issues.
> From: Rimantas Liubertas <rimantas at gmail.com>
> To: swift-users <swift-users at swift.org>
> Subject: Re: [swift-users] Parsing Decimal values from JSON
> > because JS doesn't know about decimals doesn't mean swift can't do better.
> JSON does not know about decimals either. http://json.org/. If you need
> some custom data type you can always pass it as a string and then handle as
> Best regards,
More information about the swift-users