Comments (3)
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!
from humps.
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!
from humps.
@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
from humps.
Related Issues (20)
- Drop support for 2.7 HOT 2
- Add coveralls to workflow
- Add `pylint` to linting tools
- ReadTheDocs build is failing after dropping Python 2.7
- Coveralls badge is not updating
- Support for Kebab case? HOT 6
- Add maximum depth option to all function to limit how deep to apply formatting
- Transition package manager from pipenv to poetry
- None and empty string input are not camelized correctly HOT 1
- decamelize incorrectly inserts _ after punctuation HOT 2
- Acronyms with inner digits do not decamelize correctly
- Add a conda-forge feedstock HOT 1
- An import issue after install and remove the 'humps' package. HOT 3
- [question] Abbreviation conversion assymetry workarounds HOT 3
- Add a humps jinja2 extension HOT 1
- Ability to build debian deb pkg HOT 2
- Complete transition into using poetry and more HOT 1
- Streamlit
- Kebabize string with spaces
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from humps.