Python JSON序列化Decimal对象

发布于 2021-02-02 23:16:03

我有一个Decimal('3.9')对象的一部分,希望将其编码为JSON字符串,看起来像{'x': 3.9}。我不在乎客户端的精度,因此浮点数很好。

有一个好的方法来将此序列化吗?JSONDecoder不接受Decimal对象,并且事先转换为float会产生{'x': 3.8999999999999999}错误,这将浪费大量带宽。

关注者
0
被浏览
78
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    子类化如何json.JSONEncoder

    class DecimalEncoder(json.JSONEncoder):
        def _iterencode(self, o, markers=None):
            if isinstance(o, decimal.Decimal):
                # wanted a simple yield str(o) in the next line,
                # but that would mean a yield on the line with super(...),
                # which wouldn't work (see my comment below), so...
                return (str(o) for o in [o])
            return super(DecimalEncoder, self)._iterencode(o, markers)
    

    然后像这样使用它:

    json.dumps({'x': decimal.Decimal('5.5')}, cls=DecimalEncoder)
    


  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    我想让大家知道,我在运行Python 2.6.5的Web服务器上尝试了MichałMarczyk的答案,并且运行良好。但是,我升级到Python 2.7,并且停止工作。我试图想到一种编码Decimal对象的方法,这就是我想出的:

    import decimal
    
    class DecimalEncoder(json.JSONEncoder):
        def default(self, o):
            if isinstance(o, decimal.Decimal):
                return float(o)
            return super(DecimalEncoder, self).default(o)
    

    希望这应该对任何遇到Python 2.7问题的人有所帮助。我测试了它,它似乎工作正常。如果有人注意到我的解决方案中的任何错误或提出了更好的方法,请告诉我。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看