-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
datetime.datetime() is not JSON serializable
error
#3947
Comments
So, we absolutely can do that, but ultimately I'm not sure that the value proposition is worth it. In 99% of use-cases the JSON argument literally only does We added it only because the simple use of JSON was a neat shortcut that most users would find helpful. For more advanced uses, we strongly recommend just rolling your own. Is that likely to work out for you? |
Yeah sounds reasonable, that's probably a line more code on my side to just
dump the json myself, and keeps the library conceptually simpler.
:+1:
…On Fri, Mar 31, 2017 at 12:24 AM, Cory Benfield ***@***.***> wrote:
So, we absolutely can do that, but ultimately I'm not sure that the value
proposition is worth it. In 99% of use-cases the JSON argument literally
only does json.dumps and then sets the Content-Type header to
application/json. If you're not setting the JSON field *and* some other
fields, you can totally replicate that behaviour yourself in two lines of
code.
We added it only because the simple use of JSON was a neat shortcut that
most users would find helpful. For more advanced uses, we strongly
recommend just rolling your own.
Is that likely to work out for you?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://github.com/kennethreitz/requests/issues/3947#issuecomment-290637261>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ARTsilP7WEVWcFaatRHlciSUbnMNIe6Iks5rrKoigaJpZM4MvKsR>
.
|
If I do a
request()
with JSON containing unserializable types, an exception is raised from thejson
module.e.g.
requests.post('http://example.com', json={'time': datetime.datetime.now()})
https://github.com/kennethreitz/requests/issues/1380 looks vaguely related, but doesn't directly cover this.
https://docs.python.org/2/library/json.html#json.JSONEncoder seems pretty well suited for this case; if it was possible to configure the json encoder class that Requests uses, then users could supply an implementation that handles their special cases.
Should be possible to either plumb the encoder through from the
request
call, or make it package-level config in some way. Would you consider a patch to make the JSONEncoder thatrequest
uses configurable?(The solution of just massaging the data is viable in many cases, but for complex dicts it is going to be quite painful.)
The text was updated successfully, but these errors were encountered: