- Cannot serialize
int
. I can see the value in not automatically serializing every possible object with a__str__
method. But, not converting an int? C'mon! - Cannot serilaize
None
. Wouldn'tNone
be the perfect value to indicate "don't serialize this attribute"?
Examples:
>>> import xml.etree.ElementTree as et >>> et.tostring(et.Element('Foo', attrib={ 'a': 1})) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 1009, in tostring ElementTree(element).write(file, encoding) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 663, in write self._write(file, self._root, encoding, {}) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 698, in _write _escape_attrib(v, encoding))) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 830, in _escape_attrib _raise_serialization_error(text) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 777, in _raise_serialization_error "cannot serialize %r (type %s)" % (text, type(text).__name__) TypeError: cannot serialize 1 (type int) >>> et.tostring(et.Element('Foo', attrib={ 'a': None})) Traceback (most recent call last): File " ", line 1, in File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 1009, in tostring ElementTree(element).write(file, encoding) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 663, in write self._write(file, self._root, encoding, {}) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 698, in _write _escape_attrib(v, encoding))) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 830, in _escape_attrib _raise_serialization_error(text) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 777, in _raise_serialization_error "cannot serialize %r (type %s)" % (text, type(text).__name__) TypeError: cannot serialize None (type NoneType)
the same with json. Python sometimes sucks, really
ReplyDeletebtw, how did you solved it
ReplyDeleteI just end up doing a string conversion for such values before handing them off to ET for serialization.
ReplyDeleteI think the problem with None is that python don't know how to convert None. Should it be '0', 'false' or ''
ReplyDeleteSo one has recursively convert hole output structure to string...
Yup. It's a pain...
ReplyDelete