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

jsonify a Decimal #835

Closed
michaelfillier opened this issue Aug 14, 2013 · 10 comments
Closed

jsonify a Decimal #835

michaelfillier opened this issue Aug 14, 2013 · 10 comments

Comments

@michaelfillier
Copy link

Currently getting a TypeError: Decimal('0.10') is not JSON serializable error when, you guessed it, I try to encode a Decimal.

I found an accepted fix here: http://stackoverflow.com/questions/4019856/decimal-to-json, but not sure where I should be implementing that within Flask. I tried it within my main app file, but it doesn't appear to be working.

@michaelfillier
Copy link
Author

I should add that I have simply been converting the Decimal with str(Decimal) before passing it into jsonify and that is currently working, but it's ugly.

@chason
Copy link

chason commented Aug 14, 2013

If you use simplejson, it includes Decimal encoding by default.

@michaelfillier
Copy link
Author

I will try that instead of jsonify. Is there anything jsonify does that is specifically beneficial vs simplejson?

@chason
Copy link

chason commented Aug 15, 2013

If you install the simplejson package, flask.jsonify will automatically use
that instead of the stdlib json library.

On Wed, Aug 14, 2013 at 3:54 PM, michaelfillier notifications@github.comwrote:

I will try that instead of jsonify. Is there anything jsonify does that is
specifically beneficial vs simplejson?


Reply to this email directly or view it on GitHubhttps://github.com//issues/835#issuecomment-22673774
.

@michaelfillier
Copy link
Author

Oh, that's great! Thanks.

pcraig3 pushed a commit to alphagov/notifications-api that referenced this issue Nov 22, 2016
Cost was returning a `Decimal`, which jsonify doesn't like.
Making it a float fixes the problem.

[Relevant issue on github](pallets/flask#835).
@mrwillis
Copy link

I will add that simplejson does not do datetime objects, while the default JSONEncoder does.

@davidism
Copy link
Member

@mrwillis Flask's json encoder uses simplejson when available. If something is not working, please report a new issue with an example.

@kennethreitz
Copy link
Contributor

I ran into this today

@kennethreitz
Copy link
Contributor

$ pipenv install simplejson fixed :)

@davidism
Copy link
Member

To further extend it, you can override Flask.json_encoder with your own subclass that handles more types.

@pallets pallets deleted a comment from alciomarhollanda Nov 7, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
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

5 participants