Skip to content

Commit

Permalink
Fix to avoid invalid JSON output
Browse files Browse the repository at this point in the history
This PR fixes a few issues:
- Returning None on empty strings
- Not returning NaN or infinity in JSON (invalid)
- We also do not strip the values assume that if it is like this in XML, that's on purpose.
  • Loading branch information
root authored and sanand0 committed Mar 11, 2018
1 parent 93011b1 commit c0a8915
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions xmljson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,29 @@ def _tostring(value):
@staticmethod
def _fromstring(value):
'''Convert XML string value to None, boolean, int or float'''
if not value:
# NOTE: Is this even possible ?
if value is None:
return None
std_value = value.strip().lower()
if std_value == 'true':

# FIXME: In XML, booleans are either 0/false or 1/true (lower-case !)
if value.lower() == 'true':
return True
elif std_value == 'false':
elif value.lower() == 'false':
return False

# FIXME: Using int() or float() is eating whitespaces unintendedly here
try:
return int(std_value)
return int(value)
except ValueError:
pass

try:
return float(std_value)
# Test for infinity and NaN values
if float('-inf') < float(value) < float('inf'):
return float(value)
except ValueError:
pass

return value

def etree(self, data, root=None):
Expand Down

0 comments on commit c0a8915

Please sign in to comment.