Skip to content
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

Closed
paultiplady opened this issue Mar 31, 2017 · 2 comments
Closed

datetime.datetime() is not JSON serializable error #3947

paultiplady opened this issue Mar 31, 2017 · 2 comments

Comments

@paultiplady
Copy link

If I do a request() with JSON containing unserializable types, an exception is raised from the json 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 that request 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.)

@Lukasa
Copy link
Member

Lukasa commented Mar 31, 2017

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?

@paultiplady
Copy link
Author

paultiplady commented Mar 31, 2017 via email

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants