Skip to content
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

camelizeKeys only rather than the full dictionary (values and keys) #294

Closed
robinwkurtz opened this issue May 2, 2023 · 3 comments
Closed

Comments

@robinwkurtz
Copy link

Is your feature request related to a problem? Please describe.
It would be nice to have the ability to only camelize a dictionary's keys, rather than all of the values as wwell.

Describe the solution you'd like
https://github.com/domchristie/humps#humpscamelizekeysobject-options -- Converts object keys to camelCase. It also converts arrays of objects.

Describe alternatives you've considered
Looping over the dictionary myself and using humps.camelize on keys only

Additional context
Thanks for the great tool!

@robinwkurtz robinwkurtz changed the title camelizeKeys camelizeKeys only rather than the full dictionary (values and keys) May 2, 2023
@ificiana
Copy link
Contributor

ificiana commented May 3, 2023

@robinwkurtz Hi, thanks for opening the issue. Sorry, but I couldn't replicate the problem. It'd be nice if you could list some use cases. I did this:

>>> d = {"s_gv": "j_db", "ir_o": "op_m"}
>>> humps.camelize(d)
{'sGv': 'j_db', 'irO': 'op_m'}

camelize does appear to be converting only the keys

However, it's worth noting that if the values include a mapping, it will convert their keys as well:

>>> d = {"s_gv": "j_db", "sub_dict": {"ir_o": "op_m"}}
>>> humps.camelize(d)
{'sGv': 'j_db', 'subDict': {'irO': 'op_m'}}

Currently, there's no way to limit this behaviour but there's an old issue requesting a similar functionality here: #233

@robinwkurtz
Copy link
Author

robinwkurtz commented May 16, 2023

Thanks for the comment @ificiana, my issue was in fact json.dumps which I was using within the camilize function.

>>> d = {"s_gv": "j_db", "ir_o": "op_m"}

>>> print('plain', d)
plain {'s_gv': 'j_db', 'ir_o': 'op_m'}

>>> print('humps', humps.camelize(d))
humps {'sGv': 'j_db', 'irO': 'op_m'}

>>> print('json', json.dumps(d))
json {"s_gv": "j_db", "ir_o": "op_m"}

>>> print('humps.json', humps.camelize(json.dumps(d)))
humps.json {"sGv":"jDb","irO":"opM"}

>>> print('json.humps', json.dumps(humps.camelize(d)))
json.humps {"sGv": "j_db", "irO": "op_m"}

I'm trying to camelize the keys of a large dataclass before sending it through a request, and I guess the combination of the two do not work well together.

The solution in my case (in the chance that someone else is doing the same), is to wrap my dataclass in an asdict first.

dict = asdict(dataclass)
data = humps.camelize(dict)
json.dumps(data, cls=DataclassEncoder)

Sorry for the issue!

@ificiana
Copy link
Contributor

Right! I see, no problem. json.dumps returns a string which camelize processes as a string in place of a mapping. So, your "json.humps" is the way to go. camelize the mapping first, then json.dumps the output. Anyway, have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants