[swift-users] Parsing Decimal values from JSON
Ireland, Evan
evan.ireland at sap.com
Tue Oct 31 16:13:33 CDT 2017
Rimantas,
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.
Such encodings can present interoperability issues with parsers (such as a typical JavaScript/JSON parser), and clearly also this is affecting Swift.
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
>
> > Swift shouldn't be forced to adhere to the limitations of JavaScript. Just
> > 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
> needed.
>
> Best regards,
> Rimantas
More information about the swift-users
mailing list