[issue23123] Only READ support for Decimal in json

Anders Rundgren report at bugs.python.org
Mon Dec 29 16:23:07 CET 2014


Anders Rundgren added the comment:

Using simplejson I got it to work!!!
I just wonder what you think of the solution:

import collections
import simplejson as json
from decimal import Decimal

class EnhancedDecimal(Decimal):
   def __str__ (self):
     return self.saved_string

   def __new__(cls, value="0", context=None):
     obj = Decimal.__new__(cls,value,context)
     obj.saved_string = value
     return obj;

jsonString = '{"t":6,"h":4.50, "g":"text","j":1.40e450}'
jsonObject = json.loads(jsonString, object_pairs_hook=collections.OrderedDict,parse_float=EnhancedDecimal)
for item in jsonObject:
  print jsonObject[item]
print json.dumps(jsonObject)

6
4.50
text
1.40e450
{"t": 6, "h": 4.50, "g": "text", "j": 1.40e450}

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23123>
_______________________________________


More information about the Python-bugs-list mailing list