Python JSON serialize a Decimal object

后端 未结 17 1342
星月不相逢
星月不相逢 2020-11-22 08:27

I have a Decimal(\'3.9\') as part of an object, and wish to encode this to a JSON string which should look like {\'x\': 3.9}. I don\'t care about p

17条回答
  •  無奈伤痛
    2020-11-22 09:22

    In my Flask app, Which uses python 2.7.11, flask alchemy(with 'db.decimal' types), and Flask Marshmallow ( for 'instant' serializer and deserializer), i had this error, every time i did a GET or POST. The serializer and deserializer, failed to convert Decimal types into any JSON identifiable format.

    I did a "pip install simplejson", then Just by adding

    import simplejson as json
    

    the serializer and deserializer starts to purr again. I did nothing else... DEciamls are displayed as '234.00' float format.

提交回复
热议问题