-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
.to_dict() and .to_json() attribute names are always camel cased #63
Comments
Hi @circulon, thanks for opening this issue. I was curious to know if this handy workaround that was posted earlier in another issue could work for you, at least in the meantime. Also including it below, just for completeness.
Then the only other change would be to update code to subclass from
|
This is a very good point, and the short answer to that is that when I was originally designing this library, it just "made sense" at the time. I.e., when dealing with JSON (which stands for JavaScript object notation) it made sense at the time to use JS convention for key names, which ideally was camelCase instead of snake_case. Of course, I can now understand why that would be confusing when working in Python, where all attribute names are snake-cased by convention. So, just adding a note, but the plan is that in the next major release (still TBD) this case will likely be addressed. Ie, Attribute or key names will be returned "un-changed" as part of the dump process, by default. For example, if attribute or field names are snake-cased, they should also be similarly snake-cased in the JSON object returned when I plan to add a milestone to track this, but I note however it will likely need to be implemented in a major version release (rather than a minor release) as this will be a "breaking" change so to speak. However, I definitely agree this is a good change to implement, also so that there is less confusion overall. |
I know it's been a while but it's 2024 and lot of changes have been made, and on the roadmap for V1 is to ensure no key transform in dump process. Accordingly, I've added a Mixin class |
Re-opening this issue because I do 100% understand where you're coming from. I've also had similar trouble lately, and realized that the design decision of That said -- the year 2024 is winding down, and on the roadmap for V1 is to ensure no key transform in dump process. Accordingly, I've added a Mixin class Feel free to follow my announcement on #153 to keep up-to-date on what's expected in V1. Thanks! |
@rnag In my case I nneded performance for serialization of complex nested dataclasses. Cheers |
Description
The documentation is confusing and notes that attributes names are returned as camel case from .to_json() regardless of the actual attribute name in the dataclass definition.
Why is this behaviour used and not just return the attributes as per the repr()?
This breaks many logic points as the returned attributes (from .to_dict and .to_json()) do not actually reflect the attribute names in the actual dataclass.
additional observations
I really like this module as it does an excellent job of handling Types that are not handled properly by the basic
json
module.Unfortunately I would now have to write a serialization hook to return the attribute names back to their original name so my projects will function as expeceted
Or build a custom mixin (which I would prefer not to do as this module is already doing this) that uses
simplejson
for better type handling than the stdjson
moduleWhat I Did
example
expected results
The repr(), .to_dict() & .to_json() should all have the same attribute names
The text was updated successfully, but these errors were encountered: