Coder Social home page Coder Social logo

Comments (3)

robinwkurtz avatar robinwkurtz commented on June 18, 2024 1

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.

ificiana avatar ificiana commented on June 18, 2024 1

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.

ificiana avatar ificiana commented on June 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.