dataclass-wizard
dataclass-wizard copied to clipboard
New Feature Idea: Keep undefined keys in a dict
Description
It would be really usefull to have a Meta to can Include undefined keys in a global dict of the dataclass (as lib dataclasses-json does with its option (undefined=Undefined.INCLUDE https://lidatong.github.io/dataclasses-json/#handle-unknown-extraneous-fields-in-json).
( I forgot: i can contribute :-) )
Hi @fletort! I certainly agree with you, I feel like this would be a useful feature to have. I had a look at the docs that were linked and I liked that dataclasses-json provides only one Meta
config and that you could set an enum to represent the action you wanted to take when an unknown JSON field was encountered in the input data - for example raise/exclude/include.
Unfortunately I feel as it is too late to update our existing Meta field raise_on_unknown_json_key
to be an Enum, even though that would be nice. Though I can confirm that the default behavior of the library is to exclude any additional JSON keys that don't map to any dataclass fields. Therefore, I do agree that the best option in this case would be to add a new flag to the Meta
config, something like catch_unknown_json_keys
(or include) for example. We could probably also set a default attribute on the dataclass, something like __extra_json_data__
, if the user doesn't define a catch-all field in the dataclass. Of course the actual implementation detail might vary, and will leave it up to whatever approach can work best.
I would also certainly welcome any contribution or PR to achieve this. If are you curious, you can check out the Contributing Guide I have added in the docs. I might need to update parts of it at some point, but hopefully it should provide a good idea of how to get started. Again, a PR to achieve this would be greatly appreciated 🙂