Hacker News new | ask | show | jobs
by mimsee 2143 days ago
Honest question. Would it differ if one used floats in json rather than floats in XML?
2 comments

I wouldn't think so, the issue is not so much float in the storage layer (although precision in converting to and from the storage layer can make for interesting complications) it's floats in the applications layer.
No it would not, but you could use decimals in xml.

Edit: looks like there is no float in xml.

Nor are there floats in JSON! Json numbers are stored in decimal with varying lengths.
Yes, but almost any standard JSON package will map a number with decimals to their internal float.

While alternative parsers exists (javascript has LosslessJSON for example), they're a pain to use.

Using strings for floats only has a tiny overhead, but it allows users to use standard JSON parsers and it signals a good practice.

If you use JSON numbers to serialize money, you create a situation where the path with the least friction is the incorrect one.

> While alternative parsers exists (javascript has LosslessJSON for example), they're a pain to use.

FWIW in Python that's as uncomplicated as

    json.loads('1.1', parse_float=decimal.Decimal)
though of course it helps tremendously that `decimal` is part of the stdlib.
Numbers in JSON are stored as strings. So as long as encoder/decoder is storing them in suitable format e.g BigDecimal no precision need be lost.
That may be technically true, but decoding sad encoding JSON without reading numbers as doubles is a special feature.