Add wrapper for decimal.Decimal objects in json.dumps()

Change-Id: I42b05af44c6b41ef15039a3e71026836c9e0ae94
Closes-bug: 1647346
This commit is contained in:
Luka Peschke
2016-12-05 14:10:24 +01:00
parent 35d25746e5
commit 03bc777cde

View File

@@ -15,6 +15,7 @@
#
# @author: Stéphane Albert
#
import decimal
import json
from cloudkitty.db import api
@@ -93,6 +94,14 @@ class StateManager(object):
return self._metadata
class DecimalJSONEncoder(json.JSONEncoder):
"""Wrapper class to handle decimal.Decimal objects in json.dumps()."""
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
return super(DecimalJSONEncoder, self).default(obj)
class DBStateManager(object):
def __init__(self, user_id, report_type, distributed=False):
self._state_name = self._gen_name(report_type, user_id)
@@ -124,4 +133,4 @@ class DBStateManager(object):
"""Set metadata attached to the state."""
self._db.set_metadata(self._state_name,
json.dumps(metadata))
json.dumps(metadata, cls=DecimalJSONEncoder))