Message176158
The json module already has too many options. No need for yet one such specialized.
>>> class number_str(float):
... def __init__(self, o):
... self.o = o
... def __repr__(self):
... return str(self.o)
...
>>> def decimal_serializer(o):
... if isinstance(o, decimal.Decimal):
... return number_str(o)
... raise TypeError(repr(o) + " is not JSON serializable")
...
>>> print(json.dumps([decimal.Decimal('0.20000000000000001')], default=decimal_serializer))
[0.20000000000000001]
You can extend this to support complex numbers, fractions, date and time, and many other custom types. Have specialized options for this would be cumbersome. |
|
Date |
User |
Action |
Args |
2012-11-23 09:32:56 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, rhettinger, pitrou, ezio.melotti, eric.araujo, skrah |
2012-11-23 09:32:56 | serhiy.storchaka | set | messageid: <1353663176.04.0.304458206702.issue16535@psf.upfronthosting.co.za> |
2012-11-23 09:32:56 | serhiy.storchaka | link | issue16535 messages |
2012-11-23 09:32:55 | serhiy.storchaka | create | |
|