Search code examples
pythondjangosimplejson

Drop in replacement for django.utils.simplejson that handles Decimal encoding?


With the deprecation and removal of django.utils.simplejson in 1.7, the suggested drop in replacement is the builtin json module. However the builtin doesn't handle encoding of Decimal fields when using dumps() like simplejson does. Using django.core.serializers.json.DjangoJSONEncoder i.e.

>>> string = json.dumps({"x", Decimal("100.000000")}, cls=DjangoJSONEncoder)
>>> '{"x": "100.000000"}'

seems to convert the decimal to a string before encoding, whereas

>>> string = simplejson.dumps({"x", Decimal("100.000000")})
>>> '{"x": 100.000000}'

Is there an exact replacement?


Solution

  • The best match for this that I've found appears to be ujson, it handles decimals just fine.