子类化如何
json.JSONEnprer?
class DecimalEnprer(json.JSONEnprer): def _iterenpre(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(DecimalEnprer, self)._iterenpre(o, markers)
然后像这样使用它:
json.dumps({'x': decimal.Decimal('5.5')}, cls=DecimalEnprer)


