Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/booleanjson.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"boolean": true,
"boolean_dict_list": [
{"boolean_dict": {"boolean": true}},
{"boolean_dict": {"boolean": false}}
],
"boolean_list": [true, false]
}
22 changes: 15 additions & 7 deletions json2xml/dicttoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="root"):

item_name = item_func(parent)

# since bool is also a subtype of number.Number and int, the check for bool
# never comes and hence we get wrong value for the xml type bool
# here, we just change order and check for bool first, because no other
# type other than bool can be true for bool check
if isinstance(obj, bool):
return convert_bool(item_name, obj, attr_type, cdata)

if isinstance(obj, (numbers.Number, str)):
return convert_kv(
key=item_name, val=obj, attr_type=attr_type, attr={}, cdata=cdata
Expand All @@ -153,9 +160,6 @@ def convert(obj, ids, attr_type, item_func, cdata, item_wrap, parent="root"):
cdata=cdata,
)

if isinstance(obj, bool):
return convert_bool(item_name, obj, attr_type, cdata)

if obj is None:
return convert_none(item_name, "", attr_type, cdata)

Expand Down Expand Up @@ -185,7 +189,14 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap):

key, attr = make_valid_xml_name(key, attr)

if isinstance(val, (numbers.Number, str)):
# since bool is also a subtype of number.Number and int, the check for bool
# never comes and hence we get wrong value for the xml type bool
# here, we just change order and check for bool first, because no other
# type other than bool can be true for bool check
if isinstance(val, bool):
addline(convert_bool(key, val, attr_type, attr, cdata))

elif isinstance(val, (numbers.Number, str)):
addline(
convert_kv(
key=key, val=val, attr_type=attr_type, attr=attr, cdata=cdata
Expand All @@ -203,9 +214,6 @@ def convert_dict(obj, ids, parent, attr_type, item_func, cdata, item_wrap):
)
)

elif isinstance(val, bool):
addline(convert_bool(key, val, attr_type, attr, cdata))

elif isinstance(val, dict):
if attr_type:
attr["type"] = get_xml_type(val)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_json2xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,11 @@ def test_bad_data(self):
with pytest.raises(InvalidDataError) as pytest_wrapped_e:
json2xml.Json2xml(decoded).to_xml()
assert pytest_wrapped_e.type == InvalidDataError

def test_read_boolean_data_from_json(self):
"""Test correct return for boolean types."""
data = readfromjson("examples/booleanjson.json")
result = json2xml.Json2xml(data).to_xml()
dict_from_xml = xmltodict.parse(result)
assert dict_from_xml["all"]["boolean"]["#text"] != 'True'
assert dict_from_xml["all"]["boolean"]["#text"] == 'true'
Loading