Skip to content

Commit ad928b7

Browse files
authored
Merge pull request #4 from robabram/ra/str-list-issue
Fix issue where list of int or str causes error
2 parents 39fef0d + 0c851d4 commit ad928b7

4 files changed

Lines changed: 60 additions & 2 deletions

File tree

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from setuptools import setup, find_packages
88

9-
__VERSION__ = "1.0.2"
9+
__VERSION__ = "1.0.4"
1010

1111
base_dir = os.path.abspath(os.path.dirname(__file__))
1212

src/python_easy_json/json_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(self, data: Union[Dict, str, None] = None, cast_types: bool = False
6767
elif isinstance(i, str):
6868
try:
6969
_tmp_data = json.loads(i)
70-
if _tmp_data:
70+
if _tmp_data and isinstance(_tmp_data, dict):
7171
_tmp.append(self._get_annot_cls(k)(_tmp_data, cast_types=cast_types,
7272
ordered=ordered))
7373
else:

tests/test_data/nested_lists.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"topping": [
3+
{
4+
"id": "5001",
5+
"type": "None"
6+
},
7+
{
8+
"id": "5002",
9+
"type": "Glazed"
10+
}
11+
],
12+
"string_list": [
13+
"abc",
14+
"def",
15+
"xyz"
16+
],
17+
"integer_list": [
18+
1,
19+
2,
20+
3
21+
]
22+
}

tests/test_json_with_lists.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# This file is subject to the terms and conditions defined in the
3+
# file 'LICENSE', which is part of this source code package.
4+
#
5+
from typing import List
6+
7+
from tests.base_test import BaseTestCase
8+
from python_easy_json import JSONObject
9+
from tests.test_object_model import CakeToppingTypeModel
10+
11+
12+
class ListTestObject(JSONObject):
13+
14+
topping: List[CakeToppingTypeModel]
15+
string_list: List[str]
16+
integer_list: List[int]
17+
18+
19+
class TestListsDict(BaseTestCase):
20+
21+
def test_data_with_lists(self):
22+
""" Test simple JSON, no type casting. """
23+
obj = ListTestObject(self.json_data.nested_lists)
24+
25+
self.assertIsInstance(obj, ListTestObject)
26+
27+
for t in obj.topping:
28+
self.assertIsInstance(t, CakeToppingTypeModel)
29+
30+
for s in obj.string_list:
31+
self.assertIsInstance(s, str)
32+
self.assertIn(s, ['abc', 'def', 'xyz'])
33+
34+
for i in obj.integer_list:
35+
self.assertIsInstance(i, int)
36+
self.assertIn(i, [1, 2, 3])

0 commit comments

Comments
 (0)