-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ParserConfig.class_factory (#549)
Closes #548
- Loading branch information
Showing
11 changed files
with
131 additions
and
31 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
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,35 @@ | ||
==================== | ||
Custom class factory | ||
==================== | ||
|
||
|
||
It's not recommended to modify the generated models. If you need to add any pre/post | ||
initialization logic or even validations you can use the parser config to override the | ||
default class factory. | ||
|
||
.. doctest:: | ||
|
||
>>> from dataclasses import dataclass | ||
>>> from xsdata.formats.dataclass.parsers import JsonParser | ||
>>> from xsdata.formats.dataclass.parsers.config import ParserConfig | ||
... | ||
>>> def custom_class_factory(clazz, params): | ||
... if clazz.__name__ == "Person": | ||
... return clazz(**{k: v.upper() for k, v in params.items()}) | ||
... | ||
... return clazz(**params) | ||
... | ||
|
||
>>> config = ParserConfig(class_factory=custom_class_factory) | ||
>>> parser = JsonParser(config=config) | ||
... | ||
>>> @dataclass | ||
... class Person: | ||
... first_name: str | ||
... last_name: str | ||
... | ||
>>> json_str = """{"first_name": "chris", "last_name": "foo"}""" | ||
... | ||
... | ||
>>> print(parser.from_string(json_str, Person)) | ||
Person(first_name='CHRIS', last_name='FOO') |
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
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