-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ujson __json__ attribute logic #12739
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
Conversation
could you add some tests specifically for this how standard is the dunder json tag? other libraries use? can u give some examples? |
Added some tests. I don't think
I can write a small section in io.html#json with example of how I am going to use it in rest api, like:
I am actually using multiple levels of such dataframe json nesting. Do you think its worth noting in docs? It's not like typical use case and not related to core functionality. |
@TurnaevEvgeny I think what people want is an easy way to do this (then we would expose
IIRC this was a very easy fix |
xref to #9166 |
@jreback Sorry, I didn't get why #9166 is referenced, seems irrelevant. I didn't completely get your reply that people want to |
serializaing nested structures that include pandas objects can almost be done now I would rather fix to_json than add a dunder method |
I would argue that |
can u post a short compelling example (with output) |
|
Is there anything I can help further? |
I am still confused what problem does this solve |
In the example above if I do the same without
if I decode this back:
stock_history columns becomes double json encoded , like |
Does that explains the problem & motivation? |
I still don't see any utility in doing that. You can simply construct the frame as you like, then serialize the entire thing.
|
See in your example
Not a big deal, but not a big df also, and no futher to_dict() higher in call stack. |
why are you putting arbitrary json in dataframes? this is not useful or performant at all you are just adding an artifice which doesn't enable much of anything, and will cause user confusion I still don't see how his is actually useful - where are you exporting this json to? and what are you doing with it? |
Ok, let me start from beginning. I am building REST api (flask application) that supposed to return json. Internally I am working with dataframes fetched from different sources or cached in memory. During processing api calls I often have 2-3 dataframe that somehow related (often not a straigthforward merge way). The options for me to output final json: 1) Loop though those 2-3 dataframes and construct giant dict and then json.dump it. 2) Use pandas dataframes, df.to_json() and build json gradually. |
cc @Komnomnomnom any thoughts here |
I'd be more in favour of supporting a default handler which can return encoded / raw json, similiar to the df.to_json(default_enc=lambda obj: json.dumps(obj)) |
@Komnomnomnom isn't that what |
No it converts an unsupported object into one that can be JSON serialised i.e. by converting to a
|
why 2 different ways? couldn't |
If The idea is that
|
closing as won't fix.
|
Ran 8463 tests in 127.338
OK (SKIP=592)
git diff upstream/master | flake8 --diff
A port of ujson 1.35 feature: object can define
__json__
attribute for custom serialization. Seeultrajson/ultrajson@a8f0f0f
result
[{"foo":{"parrot": 42.0}}]