Skip to content

Commit f33daea

Browse files
authored
Merge pull request #85 from jyothish6190/json-serialization-msue-67
json serialization - bug fixed
2 parents 03e404e + c33e403 commit f33daea

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

sift/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
See: https://siftscience.com/docs/references/events-api
33
"""
44

5+
import decimal
56
import json
67
import requests
78
import requests.auth
@@ -26,6 +27,11 @@ def _quote_path(s):
2627
# optional arg to override this
2728
return urllib.parse.quote(s, '')
2829

30+
class DecimalEncoder(json.JSONEncoder):
31+
def default(self, o):
32+
if isinstance(o, decimal.Decimal):
33+
return (str(o),)
34+
return super(DecimalEncoder, self).default(o)
2935

3036
class Client(object):
3137

@@ -165,7 +171,7 @@ def track(
165171
try:
166172
response = self.session.post(
167173
path,
168-
data=json.dumps(properties),
174+
data=json.dumps(properties, cls=DecimalEncoder),
169175
headers=headers,
170176
timeout=timeout,
171177
params=params)

tests/test_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import unittest
55
import warnings
6+
from decimal import Decimal
67

78
import mock
89
import requests.exceptions
@@ -19,7 +20,7 @@ def valid_transaction_properties():
1920
return {
2021
'$buyer_user_id': '123456',
2122
'$seller_user_id': '654321',
22-
'$amount': 1253200,
23+
'$amount': Decimal('1253200.0'),
2324
'$currency_code': 'USD',
2425
'$time': int(datetime.datetime.now().strftime('%s')),
2526
'$transaction_id': 'my_transaction_id',

tests/test_client_v203.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
from decimal import Decimal
23
import warnings
34
import json
45
import mock
@@ -16,7 +17,7 @@ def valid_transaction_properties():
1617
return {
1718
'$buyer_user_id': '123456',
1819
'$seller_user_id': '654321',
19-
'$amount': 1253200,
20+
'$amount': Decimal('1253200.0'),
2021
'$currency_code': 'USD',
2122
'$time': int(datetime.datetime.now().strftime('%s')),
2223
'$transaction_id': 'my_transaction_id',

0 commit comments

Comments
 (0)