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/", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/", line 264, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/", 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 <>
Python-bugs-list mailing list

Reply via email to