Skip to content

Commit e3b296c

Browse files
xrmxmistercrunch
authored andcommittedJan 5, 2017
utils: teach our json serializer to handle more types (#1907)
Namely datetime.time and numpy.bool_ Refs: #1900 Refs: #1903
1 parent c2d29fb commit e3b296c

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed
 

‎superset/utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import unicode_literals
66

77
from builtins import object
8-
from datetime import date, datetime
8+
from datetime import date, datetime, time
99
import decimal
1010
import functools
1111
import json
@@ -216,6 +216,8 @@ def base_json_conv(obj):
216216

217217
if isinstance(obj, numpy.int64):
218218
return int(obj)
219+
elif isinstance(obj, numpy.bool_):
220+
return bool(obj)
219221
elif isinstance(obj, set):
220222
return list(obj)
221223
elif isinstance(obj, decimal.Decimal):
@@ -239,6 +241,8 @@ def json_iso_dttm_ser(obj):
239241
obj = obj.isoformat()
240242
elif isinstance(obj, date):
241243
obj = obj.isoformat()
244+
elif isinstance(obj, time):
245+
obj = obj.isoformat()
242246
else:
243247
raise TypeError(
244248
"Unserializable object {} of type {}".format(obj, type(obj))

‎tests/utils_tests.py

+47-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
1-
from datetime import datetime, date, timedelta
2-
from superset import utils
3-
import unittest
4-
5-
from mock import Mock, patch
6-
7-
class UtilsTestCase(unittest.TestCase):
8-
def test_json_int_dttm_ser(self):
9-
dttm = datetime(2020, 1, 1)
10-
ts = 1577836800000.0
11-
json_int_dttm_ser = utils.json_int_dttm_ser
12-
assert json_int_dttm_ser(dttm) == ts
13-
assert json_int_dttm_ser(date(2020, 1, 1)) == ts
14-
assert json_int_dttm_ser(datetime(1970, 1, 1)) == 0
15-
assert json_int_dttm_ser(date(1970, 1, 1)) == 0
16-
assert json_int_dttm_ser(dttm + timedelta(milliseconds=1)) == (ts + 1)
17-
18-
with self.assertRaises(TypeError):
19-
utils.json_int_dttm_ser("this is not a date")
20-
21-
@patch('superset.utils.datetime')
22-
def test_parse_human_timedelta(self, mock_now):
23-
mock_now.return_value = datetime(2016, 12, 1)
24-
self.assertEquals(utils.parse_human_timedelta('now'), timedelta(0))
1+
from datetime import datetime, date, timedelta, time
2+
from decimal import Decimal
3+
from superset.utils import (
4+
json_int_dttm_ser, json_iso_dttm_ser, base_json_conv, parse_human_timedelta
5+
)
6+
import unittest
7+
import uuid
8+
9+
from mock import Mock, patch
10+
import numpy
11+
12+
13+
class UtilsTestCase(unittest.TestCase):
14+
def test_json_int_dttm_ser(self):
15+
dttm = datetime(2020, 1, 1)
16+
ts = 1577836800000.0
17+
assert json_int_dttm_ser(dttm) == ts
18+
assert json_int_dttm_ser(date(2020, 1, 1)) == ts
19+
assert json_int_dttm_ser(datetime(1970, 1, 1)) == 0
20+
assert json_int_dttm_ser(date(1970, 1, 1)) == 0
21+
assert json_int_dttm_ser(dttm + timedelta(milliseconds=1)) == (ts + 1)
22+
23+
with self.assertRaises(TypeError):
24+
json_int_dttm_ser("this is not a date")
25+
26+
def test_json_iso_dttm_ser(self):
27+
dttm = datetime(2020, 1, 1)
28+
dt = date(2020, 1, 1)
29+
t = time()
30+
assert json_iso_dttm_ser(dttm) == dttm.isoformat()
31+
assert json_iso_dttm_ser(dt) == dt.isoformat()
32+
assert json_iso_dttm_ser(t) == t.isoformat()
33+
34+
with self.assertRaises(TypeError):
35+
json_iso_dttm_ser("this is not a date")
36+
37+
def test_base_json_conv(self):
38+
assert isinstance(base_json_conv(numpy.bool_(1)), bool) == True
39+
assert isinstance(base_json_conv(numpy.int64(1)), int) == True
40+
assert isinstance(base_json_conv(set([1])), list) == True
41+
assert isinstance(base_json_conv(Decimal('1.0')), float) == True
42+
assert isinstance(base_json_conv(uuid.uuid4()), str) == True
43+
44+
@patch('superset.utils.datetime')
45+
def test_parse_human_timedelta(self, mock_now):
46+
mock_now.return_value = datetime(2016, 12, 1)
47+
self.assertEquals(parse_human_timedelta('now'), timedelta(0))

0 commit comments

Comments
 (0)
Please sign in to comment.