forked from XeroAPI/xero-python-oauth2-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
30 lines (21 loc) · 725 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
import json
import uuid
from datetime import datetime, date
from decimal import Decimal
from xero_python.api_client.serializer import serialize
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime):
return o.isoformat()
if isinstance(o, date):
return o.isoformat()
if isinstance(o, (uuid.UUID, Decimal)):
return str(o)
return super(JSONEncoder, self).default(o)
def parse_json(data):
return json.loads(data, parse_float=Decimal)
def serialize_model(model):
return jsonify(serialize(model))
def jsonify(data):
return json.dumps(data, sort_keys=True, indent=4, cls=JSONEncoder)