Skip to content

Commit 65eef00

Browse files
Vinit Kumarvinitkumar
authored andcommitted
fix: issue with wrong output for boolean list
1 parent b32d38e commit 65eef00

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

examples/booleanjson2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"boolean_list": [true, false],
3+
"number_array": [1, 2, 3],
4+
"string_array": ["a", "b", "c"]
5+
}

json2xml/dicttoxml.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="root"):
137137

138138
LOG.info(f'Inside convert(). obj type is: "{type(obj).__name__}", obj="{str(obj)}"')
139139

140+
140141
item_name = item_func(parent)
141142

142143
# since bool is also a subtype of number.Number and int, the check for bool
@@ -265,7 +266,11 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata, item_wrap):
265266
f'Looping inside convert_list(): item="{str(item)}", item_name="{item_name}", type="{type(item).__name__}"'
266267
)
267268
attr = {} if not ids else {"id": f"{this_id}_{i + 1}"}
268-
if isinstance(item, (numbers.Number, str)):
269+
270+
if isinstance(item, bool):
271+
addline(convert_bool(item_name, item, attr_type, attr, cdata))
272+
273+
elif isinstance(item, (numbers.Number, str)):
269274
if item_wrap:
270275
addline(
271276
convert_kv(
@@ -298,8 +303,6 @@ def convert_list(items, ids, parent, attr_type, item_func, cdata, item_wrap):
298303
)
299304
)
300305

301-
elif isinstance(item, bool):
302-
addline(convert_bool(item_name, item, attr_type, attr, cdata))
303306

304307
elif isinstance(item, dict):
305308
item_dict_str = convert_dict(

tests/test_json2xml.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,12 @@ def test_read_boolean_data_from_json(self):
184184
dict_from_xml = xmltodict.parse(result)
185185
assert dict_from_xml["all"]["boolean"]["#text"] != 'True'
186186
assert dict_from_xml["all"]["boolean"]["#text"] == 'true'
187+
188+
189+
def test_read_boolean_data_from_json2(self):
190+
"""Test correct return for boolean types."""
191+
data = readfromjson("examples/booleanjson2.json")
192+
result = json2xml.Json2xml(data).to_xml()
193+
dict_from_xml = xmltodict.parse(result)
194+
assert dict_from_xml["all"]["item"][0]["#text"] != 'True'
195+
assert dict_from_xml["all"]["item"][0]["#text"] == 'true'

0 commit comments

Comments
 (0)