sanand0 / xmljson

xmlsjon converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
MIT License
121 stars 33 forks source link

"NAN" to invalid json #23

Closed Sasha4ever closed 7 years ago

Sasha4ever commented 7 years ago
>>> from lxml import etree
>>> from xmljson import XMLData
>>> XMLData(attr_prefix=None, dict_type=dict).data(etree.XML('<ROOT test="NAN"/>'))
{'ROOT': {'test': nan}}
Sasha4ever commented 7 years ago

because

>>> float('nan')
nan
>>> float('inf')
inf
>>> float('-inf')
-inf
sanand0 commented 7 years ago

@Sasha4ever - thanks. This raises a broader question:

Because of the variety of possible conversions, you can pass an xml_fromstring that uses the conversion method you prefer. For example:

>>> from lxml import etree
>>> from xmljson import XMLData
>>> XMLData(xml_fromstring=str, attr_prefix=None, dict_type=dict).data(etree.XML('<ROOT test="NAN"/>'))
{'ROOT': {'test': 'NAN'}}

Hope that resolves the issue.

Sasha4ever commented 7 years ago

IMHO XMLData shouldn't return invalid XML.

dagwieers commented 7 years ago

@Sasha4ever I don't see from your comments that XMLData is returning invalid XML. Can you elaborate ?

Ah, I think you mean return invalid JSON. Yes, that requires a fix.

The question is what would you like for these values in the resulting JSON output. To me it's unclear whether we want to fail the conversion, make it null or keep it as a string. Any insight you want to share ?