eclipse-basyx / basyx-python-sdk

MIT License
60 stars 28 forks source link

`adapter.{json,xml}`: make (de-)serialization interfaces coherent #251

Closed jkhsjdhjs closed 7 months ago

jkhsjdhjs commented 7 months ago

lxml supports paths already, no modification is necessary there. However, the lxml.etree.ElementTree.write() function requires BinaryIO, i.e. files opened with the 'b' mode. While it would be possible to access the underlying binary buffer of files opened in text mode via open(), this isn't possible for io.StringIO(), as it doesn't have the buffer property. Thus, even if we could support files opened via open() in text mode, we couldn't annotate the XML serialization functions with TextIO, as io.StringIO() remains unsupported. Because of that, I decided to not support TextIO for the XML serialization.

The builtin JSON module only supports file handles, with the json.dump() method only supporting TextIO and json.load() supporting TextIO and BinaryIO. Thus, the JSON adapter is modified to open() given paths, while the JSON serialization is additionally modified to wrap BinaryIO with io.TextIOWrapper.

Fix https://github.com/eclipse-basyx/basyx-python-sdk/issues/42

s-heppner commented 7 months ago

LGTM, that might make the integration of aas-core easier as well.