.to_dict() and .to_json() attribute names are always camel cased
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 250
- Forks
- 36
- PR merge metrics
- No merged PRs in 30d
Description
- Dataclass Wizard version: 0.22.1
- Python version: 3.9
- Operating System: Mac os X 12.4
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 std json module
What I Did
example
from dataclasses import dataclass
from typing import Optional
from dataclass_wizard import JSONWizard
@dataclass
class Something(JSONWizard):
user_id: Optional[str]
access_token: str
expires: int
some_type: str
some = Something(
expires=3600,
some_type="hello",
user_id="1235-1235",
access_token="abcd-12345-hjgas-12365",
)
>>> print(f"class reps: {repr(some)}")
class reps: Something(user_id='1235-1235', access_token='abcd-12345-hjgas-12365', expires=3600, some_type='hello')
>>> print(f"class dict: {some.to_dict()}")
class dict: {'userId': '1235-1235', 'accessToken': 'abcd-12345-hjgas-12365', 'expires': 3600, 'someType': 'hello'}
>>> print(f"class json: {some.to_json()}")
class json: {"userId": "1235-1235", "accessToken": "abcd-12345-hjgas-12365", "expires": 3600, "someType": "hello"}
>>>
expected results
The repr(), .to_dict() & .to_json() should all have the same attribute names
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the JSONWizard.to_dict() and to_json() entry points described in the example, and compare their output with the dataclass repr(). Review the issue discussion for the intended naming behavior and configuration expectations. Done means serialization returns the original dataclass attribute names consistently, with behavior verified for the shown fields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100