-
Notifications
You must be signed in to change notification settings - Fork 353
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
Add coerce_to_json function #94
base: master
Are you sure you want to change the base?
Conversation
Absolutely! This is certainly very interesting! I'm curious, can you go into a bit of detail about where and how you've applied this technique? As for structural changes, a minor point would mostly be to "vendor in" the |
I tend to use _set_s for containers and (especially for test fixtures) types.MappingProxyType, the read-only dict proxy, both of which play havoc with JSON. I am also lazy to the point of masochism and will spend hours developing a general-purpose solution rather than do the same thing twice. For example, I have a client for an HTTP API that updates groups given a list of identifiers to either add or remove, so I build the list of identifiers that should be present (from a source which will have duplicates), request the current identifiers in the group, do set operations for the adds and removes and then throw the sets of adds and removes at my client. The only downside is that instead of passing the data to requests.requests as
Alternatively, I could use |
For the vendoring-in of first, is_scalar (and consequently is_iterable), would you prefer them to be:
So many questions for so few lines... Guess I should research what's already being done. |
Tuples use a teensy bit less memory but, perhaps more importantly, communicate the intention of immutability of this collection.
I looked at the existing functions and did not find too many examples other than make_sentinel, so I've done what seems best to me; let me know if this is the preferred approach. |
A class having to an explicit conversion method and also being iterable seems a likely case, so test for conversion methods before attempting type-guessing.
This is a function that tries to map data to one of the handful of primitive types supported by JSON. I have included tests using it both for the default parameter of
json.dump
and withremap
.There is a bit more work to be done but I wanted to first see if there way any interest in it before I put more effort into it. (I am also open to suggestions for the name.)