Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged

Conversation

jkhsjdhjs
Copy link
Contributor

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 #42

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 #42
... by using `StringIO` instead of `BytesIO`.
@s-heppner
Copy link
Contributor

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

@s-heppner s-heppner merged commit d32349c into eclipse-basyx:main Mar 14, 2024
8 checks passed
@s-heppner s-heppner deleted the feature/coherent_adapter_interfaces branch March 14, 2024 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect parameter type hint or method realization of write_aas_xml_file()
2 participants