From: Zooko O'Whielacronx on 18 Jun 2010 13:39
I've uploaded a small package to PyPI which is a small wrapper around
simplejson that sets the default behavior so that JSON decimal values
are mapped to type Decimal instead of type float:
It has pretty thorough unit tests, including a copy of all the
simplejson unit tests, which I copied into the jsonutil package, as
well as a few added tests to test the new functionality. I also did
some benchmarks to satisfy myself that using jsonutil isn't noticeably
slower than using simplejson. You can find the benchmarking code in
the package, but I'm not sure if it comes with documentation
sufficient to let you easily run the benchmarks yourself.
I did this originally on behalf of my employer, SimpleGeo, who now use
this wrapper in our production system.
Based on my reasoning and also SimpleGeo's experience, this introduced
absolutely no compatibility problems between our service (an HTTP API)
and users of our service, who themselves parse and construct JSON
however they want (typically from other languages like Ruby and PHP).
This is because no user of our API sends a string like "0.03" and then
expects to get back "0.029999999999999999" and breaks in some way if
we instead send back "0.03".
If there were clients like that, then they would have broken when we
made this change, because formerly, using simplejson, we would have
accepted "0.03" and then sent back "0.029999999999999999", but now,
using jsonutil, we accept "0.03" and send back "0.03".
I would expect clients that breaks in such a case to be rare, but of
course computer systems are always surprising me with their creative
ways to be fragile and failure-prone, so I wouldn't bet against a few
such systems existing. If anyone spots one in the wild, please let me
Converting from simplejson (default float) to jsonutil (default
Decimal) did, as expected, cause type mismatches internally inside our
server, where some calling code was expecting float and the called
code was returning Decimal, or vice versa. So far these problems were
all "shallow" in that they could be fixed by adding a cast or by
changing one side or the other, as desired.
In short, I advise people not to fear using Python Decimals to manage
their JSON decimal strings. It will probably introduce zero
incompatibilities across the wire, and any type mismatches it
introduces within your own code base will probably be easy to fix.