import json import decimal class DecimalEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, decimal.Decimal): return { "_type": "decimal", "value": str(obj) } return super(DecimalEncoder, self).default(obj) class DecimalDecoder(json.JSONDecoder): def __init__(self, *args, **kwargs): json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs) def object_hook(self, obj): if '_type' not in obj: return obj type = obj['_type'] if type == 'decimal': return decimal.Decimal(obj['value']) return obj