Closed gregLibert closed 7 years ago
@gregLibert this feature already exists. You can override the conversion with the xml_fromstring
option. For example:
>>> def convert_only_int(val):
... return int(val) if val.isdigit() else val
>>> bf_int = BadgerFish(xml_fromstring=convert_only_int)
>>> dumps(bf_int.data(fromstring('<p><x>1</x><y>2.5</y><z>NaN</z></p>')))
'{"p": {"x": {"$": 1}, "y": {"$": "2.5"}, "z": {"$": "NaN"}}}'
In the example above, only the integer 1
is converted to an integer. Everything else is retained as a
float. You can write your own custom function.
Does this meet your needs?
Great! This is exactly what I'm looking for. I'm using the following conversion rule : try: return int(val) except ValueError: pass try: float_val = float(val) if(float_val == float_val): return float_val except ValueError: pass return val Thank you very much !
Hi. Python float function silently convert string like "NAN" or "Infinity" to nan or inf. I would like to keep those value as string. If I fork your project and add a function parameter (as strick_number for instance) would you be interested ? By the way, regarding the JSON specification true NaN or Inf should be converted to Null, I can also do that if you wish (strick_json?).
Grégory