Skip to content

Commit a007d80

Browse files
Merge pull request #9 from jstol/json-decimal-encode
Added in support for `Decimal` objects in SMTP API JSON messages
2 parents d2a355e + 9ec7a4d commit a007d80

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

smtpapi/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import json
1+
import json, decimal
22

3+
class _CustomJSONEncoder(json.JSONEncoder):
4+
5+
def default(self, o):
6+
if isinstance(o, decimal.Decimal):
7+
return float(o)
8+
# Provide a fallback to the default encoder if we haven't implemented special support for the object's class
9+
return super(_CustomJSONEncoder, self).default(o)
310

411
class SMTPAPIHeader(object):
512

@@ -88,4 +95,4 @@ def json_string(self):
8895
for key in self.data.keys():
8996
if self.data[key] != [] and self.data[key] != {}:
9097
result[key] = self.data[key]
91-
return json.dumps(result)
98+
return json.dumps(result, cls=_CustomJSONEncoder)

test/__init__.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import unittest
2-
import json
1+
import unittest, json, decimal
32
from smtpapi import SMTPAPIHeader
43

5-
64
class TestSMTPAPI(unittest.TestCase):
75

86
def setUp(self):
97
self.validHeader = json.loads('''{"to":["[email protected]",
108
11-
"sub":{"subKey":["subValue"]},
9+
"sub":{"subKey":["subValue"],"decimalKey":[1.23456789]},
1210
"section":{"testSection":"sectionValue"},
1311
"category":["testCategory"],
1412
"unique_args":{"testUnique":"uniqueValue"},
@@ -19,7 +17,7 @@ def setUp(self):
1917
"filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}''')
2018

2119
self.dropsHeader = json.loads('''{
22-
"sub":{"subKey":["subValue"]},
20+
"sub":{"subKey":["subValue"],"decimalKey":[1.23456789]},
2321
"unique_args":{"testUnique":"uniqueValue"},
2422
"filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}''')
2523

@@ -28,6 +26,7 @@ def test_add(self):
2826
header.add_to('[email protected]')
2927
header.add_to(['[email protected]', '[email protected]'])
3028
header.add_substitution('subKey', 'subValue')
29+
header.add_substitution('decimalKey', decimal.Decimal("1.23456789"))
3130
header.add_section('testSection', 'sectionValue')
3231
header.add_category('testCategory')
3332
header.add_unique_arg('testUnique', 'uniqueValue')
@@ -42,7 +41,10 @@ def test_add(self):
4241
def test_set(self):
4342
header = SMTPAPIHeader()
4443
45-
header.set_substitutions(json.loads('{"subKey":["subValue"]}'))
44+
header.set_substitutions({
45+
"subKey": ["subValue"],
46+
"decimalKey": [decimal.Decimal("1.23456789")]
47+
})
4648
header.set_sections(json.loads('{"testSection":"sectionValue"}'))
4749
header.set_categories(["testCategory"])
4850
header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}'))
@@ -56,7 +58,10 @@ def test_set(self):
5658
def test_drop_empty(self):
5759
header = SMTPAPIHeader()
5860
header.set_tos([])
59-
header.set_substitutions(json.loads('{"subKey":["subValue"]}'))
61+
header.set_substitutions({
62+
"subKey": ["subValue"],
63+
"decimalKey": [decimal.Decimal("1.23456789")]
64+
})
6065
header.set_sections(json.loads('{}'))
6166
header.set_categories([])
6267
header.set_unique_args(json.loads('{"testUnique":"uniqueValue"}'))

0 commit comments

Comments
 (0)