[New-bugs-announce] [issue16535] json encoder unable to handle decimal

Éric Araujo report at bugs.python.org
Thu Nov 22 23:45:38 CET 2012

New submission from Éric Araujo:

In 2.7 and other versions, the json module has incomplete support for decimals:

>>> json.loads('0.2', parse_float=Decimal)
>>> json.dumps(json.loads('0.2', parse_float=Decimal))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('0.2') is not JSON serializable

simplejson encodes decimals out of the box, but json can’t round-trip.

messages: 176135
nosy: eric.araujo, ezio.melotti, pitrou, rhettinger
priority: normal
severity: normal
status: open
title: json encoder unable to handle decimal
type: behavior

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list