-
Notifications
You must be signed in to change notification settings - Fork 158
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
[FEATURE] Overwrite encoder via to_dict? #451
Comments
@powellnorma We'll start v1 API next week - please take a look at #442, it should solve your problem. Re implementing something in v0, I'm not sure its worth the time. |
Hm ok, iiuc in v1 there will be no classes we can inherit for a dataclasses_json.cfg.global_config.encoders[DataPerson] = encode_data_person Or will it be possible to define the encoding behavior in the class itself (without setting Also, will one only need to set dataclasses_json.cfg.global_config.encoders[DataPerson] = encode_data_person or also dataclasses_json.cfg.global_config.encoders[list[DataPerson]] = .. etc? Thank you! |
Basically you will need to define a serializer for the type in question and register it with smth like |
Thank you. Would it make sense to let the base serializer check for a special method like |
This, most likely. I'll make sure to ping you on related PRs and readme updates when time comes :) |
Description
It can be useful to supply a custom encoder function (e.g. for #176 (comment)). Being able to do that via overwriting
to_dict
seems intuitive. However it looks liketo_dict
is not called for nested classes?Possible solution
Check if object is a
DataClassJsonMixin
, and if so useto_dict
while encodingAlternatives
One can already do the following:
However current
to_dict
nested behavior may be unintuitive for new users + this is more verbose.Also this does not generalize to related type annotations, as the key used in
encoders
has to match exactly the type annotation. E.g. if a classC
hasall_a: list[A]
, we would have to add:One could in this case also use
field(metadata=config(..))
But e.g. when wanting to add additional attributes (as in Support properties #176) this is not enough
Context
No response
The text was updated successfully, but these errors were encountered: