Comments (6)
A related idea is to have the alias(es) depend on the dialect
. Depending on the scenario, this may be a somewhat cleaner solution to the above example problem, e.g., if you want to deserialize in of the two ways. It would also help in scenarios where the key used may depend on natural language, e.g., in a scenario where a config file may be given in multiple languages (e.g., it has a key "date" and a German alias "Datum").
@Fatal1ty Do you think this would be a reasonable feature to have?
edit: A less elegant thing would be to handle such things via __post_deserialize__
or __pre_deserialize__
hooks, but unfortunately, they don't take a context
. (Is there a specific reason to have the context only for serialization?)
from mashumaro.
@real-or-random In short, I like your idea.
I also thought about configuring aliases in dialects but in a more complex scenario. Right now, it's not possible to use the aliases
config option for nested structures, which is why I haven't mirrored it in dialects yet. If there is only one dialect, it would be useful to have aliases for all the fields including those nested in dataclasses or other structures. I imagine something like foo.bar[*].date
for a key date
in a dictionary-like structure (dataclasses, TypedDicts) within a sequence bar
of a dictionary-like structure in the key foo
. Or it could be [*].date
for a key date
of any dictionary-like structure within a sequence when the dialect is being used for codecs that work not only with top level dataclasses.
If you'd like to contribute, we could start with the simple scenario and add support for a path syntax later.
from mashumaro.
Hi @CralixRaev
This would be a useful feature. I think reusing existing alias
field option and aliases
config option is a good way to go.
The field option would accept Union[str, Sequence[str]]
instead of current str
. The config option would accept Dict[str, Union[str, Sequence[str]]]
instead of current Dict[str, str]
Would you like to work on it? It will be pretty straightforward after upcoming merge of the related PR:
from mashumaro.
edit: A less elegant thing would be to handle such things via
__post_deserialize__
or__pre_deserialize__
hooks, but unfortunately, they don't take a context. (Is there a specific reason to have the context only for serialization?)
Actually, no specific reason here. It was requested just for serialization.
from mashumaro.
If you'd like to contribute, we could start with the simple scenario and add support for a path syntax later.
Thanks for the swift reply! I might need this in the future, but it's further down the road. I'm still in the process of figuring out whether this lib is the right for me, see my other issue. ;)
from mashumaro.
Thanks for the swift reply! I might need this in the future, but it's further down the road. I'm still in the process of figuring out whether this lib is the right for me, see my other issue. ;)
Thank you for your feedback and illustrative examples with German words. This will help me to prioritize what needs to be done first.
from mashumaro.
Related Issues (20)
- "`dict[str, T]` as a field type is not supported" HOT 2
- InitVar with no default value HOT 6
- The types defined inside the function result in syntactically invalid generated code
- Allow for more complex logic for subclass Discrimnator field matching HOT 3
- TypedDicts not working with `from __future__ import annotations` HOT 2
- omit_default breaks IntFlag serialization HOT 1
- Using Union with int/float casts to whichever appears first HOT 3
- Not parsing Generics correctly HOT 2
- Unserializable field in 3.12 if defined as a Generic TypeVar with mixin bounds HOT 6
- Allow propagation of class based discriminator settings to subclasses HOT 3
- Reject extra keys on deserialization HOT 7
- Investigate support for recursive Union types HOT 2
- Supports `numpy.ndarray` type for `orjson` HOT 2
- Add support for PEP 695
- Union type of [int | float] not serialized correctly HOT 2
- Add an alternative way to assign a field alias with annotations
- Add support for PEP 696
- Annotated SerializationStrategy used as a field serialization strategy leads to RecursionError
- Suppress warning about Union type HOT 4
- Take description from docstring HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mashumaro.