-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add globalns support via SerializerConfig (#724)
Co-authored-by: Simon Schiele <[email protected]>
- Loading branch information
1 parent
351464d
commit 751198b
Showing
12 changed files
with
188 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
=================== | ||
Custom type mapping | ||
=================== | ||
|
||
When managing a big collection of models, it sometimes is tricky to split them | ||
into multiple python modules. Even more so if they depend on each other. For | ||
the models to be serializable by xsdata, they need to be able to import all | ||
other referenced models, which might not be possible due to circular imports. | ||
|
||
One solution to get around this problem is to fence the imports within the | ||
python modules by using :data:`python:typing.TYPE_CHECKING` and passing a | ||
dedicated type-map dictionary to the | ||
:class:`~xsdata.formats.dataclass.serializers.config.SerializerConfig`. | ||
|
||
|
||
.. tab:: city.py | ||
|
||
.. literalinclude:: /../tests/models/typemapping/city.py | ||
:language: python | ||
|
||
.. tab:: street.py | ||
|
||
.. literalinclude:: /../tests/models/typemapping/street.py | ||
:language: python | ||
|
||
.. tab:: house.py | ||
|
||
.. literalinclude:: /../tests/models/typemapping/house.py | ||
:language: python | ||
|
||
|
||
By fencing the imports, we are able to keep our models in different python | ||
modules that are cleanly importable and considered valid by static type | ||
checkers. | ||
|
||
Passing the type-map dictionary, which maps the class/model-names directly to | ||
imported objects, enables xsdata to serialize the models. | ||
|
||
|
||
.. testcode:: | ||
|
||
from xsdata.formats.dataclass.serializers import XmlSerializer | ||
from xsdata.formats.dataclass.serializers.config import SerializerConfig | ||
|
||
from tests.models.typemapping.city import City | ||
from tests.models.typemapping.house import House | ||
from tests.models.typemapping.street import Street | ||
|
||
|
||
city1 = City(name="footown") | ||
street1 = Street(name="foostreet") | ||
house1 = House(number=23) | ||
city1.streets.append(street1) | ||
street1.houses.append(house1) | ||
|
||
type_map = {"City": City, "Street": Street, "House": House} | ||
serializer_config = SerializerConfig(pretty_print=True, globalns=type_map) | ||
|
||
xml_serializer = XmlSerializer(config=serializer_config) | ||
serialized_house = xml_serializer.render(city1) | ||
print(serialized_house) | ||
|
||
|
||
.. testoutput:: | ||
|
||
<?xml version="1.0" encoding="UTF-8"?> | ||
<City> | ||
<name>footown</name> | ||
<streets> | ||
<name>foostreet</name> | ||
<houses> | ||
<number>23</number> | ||
</houses> | ||
</streets> | ||
</City> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from unittest import TestCase | ||
|
||
from tests.models.typemapping.city import City | ||
from tests.models.typemapping.house import House | ||
from tests.models.typemapping.street import Street | ||
from xsdata.formats.dataclass.serializers import JsonSerializer | ||
from xsdata.formats.dataclass.serializers import PycodeSerializer | ||
from xsdata.formats.dataclass.serializers import XmlSerializer | ||
from xsdata.formats.dataclass.serializers.config import SerializerConfig | ||
|
||
|
||
class TypeMappingTests(TestCase): | ||
def test_type_mapping(self): | ||
city1 = City(name="footown") | ||
street1 = Street(name="foostreet") | ||
house1 = House(number=23) | ||
city1.streets.append(street1) | ||
street1.houses.append(house1) | ||
|
||
type_mapping = {"City": City, "Street": Street, "House": House} | ||
serializer_config = SerializerConfig(globalns=type_mapping) | ||
|
||
json_serializer = JsonSerializer(config=serializer_config) | ||
xml_serializer = XmlSerializer(config=serializer_config) | ||
pycode_serializer = PycodeSerializer(config=serializer_config) | ||
|
||
for model in (city1, street1, house1): | ||
json_serializer.render(model) | ||
xml_serializer.render(model) | ||
pycode_serializer.render(model) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from dataclasses import dataclass | ||
from dataclasses import field | ||
from typing import List | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from tests.models.typemapping.street import Street | ||
|
||
|
||
@dataclass | ||
class City: | ||
class Meta: | ||
global_type = False | ||
|
||
name: str | ||
streets: List["Street"] = field(default_factory=list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from tests.models.typemapping.street import Street | ||
|
||
|
||
@dataclass | ||
class House: | ||
class Meta: | ||
global_type = False | ||
|
||
number: int | ||
street: Optional["Street"] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dataclasses import dataclass | ||
from dataclasses import field | ||
from typing import List | ||
from typing import Optional | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from tests.models.typemapping.city import City | ||
from tests.models.typemapping.house import House | ||
|
||
|
||
@dataclass | ||
class Street: | ||
class Meta: | ||
global_type = False | ||
|
||
name: str | ||
city: Optional["City"] = None | ||
houses: List["House"] = field(default_factory=list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters