Fix #220: Adds a default JSONEncoder implementation to Flask.json_encoder_class #471

Closed
wants to merge 37 commits into
from

Conversation

Projects
None yet
Contributor

jfinkels commented Apr 1, 2012

Fix for issue #220 which adds a default JSONEncoder implementation to Flask.json_encoder_class, which users can override with their own implementation. Also makes jsonify use current_app.json_encoder_class as its encoder class. This is solution 1 as proposed by @jparise.

This solution asks users to subclass flask.JSONEncoder, which is now exposed as part of the public API for Flask. flask.JSONEncoder simply serializes datetime objects to ISO 8601 format strings. It can be changed in the future if some other data type becomes common, but I believe this is a reasonable default.

@jfinkels jfinkels Fix #220: adds a default JSONEncoder implementation to Flask.json_enc…
…oder_class, which users can override with their own implementation; makes jsonify use current_app.json_encoder_class as its encoder class.
a7d3059
Contributor

rduplain commented Apr 1, 2012

+1, looks good to me.

Contributor

jparise commented Apr 1, 2012

This looks good to me, too. Thanks for doing the work!

Contributor

jfinkels commented Apr 2, 2012

My pleasure. I want all these JSON features in Flask for my own projects!

Contributor

jfinkels commented Apr 26, 2012

Just merged in changes from pull request #502, which makes the jsonify more elegant, and fixes the issue in which we were only providing the custom JSON encoder if padded was not specified.

P.S. I'd love to see this in 0.9...

which will be released soon I hope...

Contributor

kennethreitz commented Jun 25, 2012

Looks like this is kind of stale now. Care to rebase?

dmishe and others added some commits Mar 1, 2012

@dmishe @jfinkels dmishe Blueprint example app 48c0d4a
@dmishe @jfinkels dmishe Blueprint example tests 59269e6
@dmishe @jfinkels dmishe Removed unneeded print statements form mongokit pattern doc 6ffacb7
@ekoka @jfinkels ekoka Update flask/app.py 070e04c
@ekoka @jfinkels ekoka Update flask/testsuite/basic.py 11e94fa
@mitsuhiko @jfinkels mitsuhiko Improved interface for the URL build error handler 8940df2
@mitsuhiko @jfinkels mitsuhiko Added required_methods e9ee9b4
@mitsuhiko @jfinkels mitsuhiko Added after_this_request decorator. c552360
@alekzvik @jfinkels alekzvik Changed docstring according to docs. 97c66e8
@msabramo @jfinkels msabramo Fix failing test: "AssertionError: 'application/javascript' != 'appli…
…cation/json'" in flask/testsuite/helpers.py", line 88
9a79a80
@msabramo @jfinkels msabramo Add .travis.yml 16188ea
@kuyan @jfinkels kuyan Emended extensiondev.rst. 3fc700d
@brousch @jfinkels brousch Added link to extensions in "Hook. Extend." section. 6e8e493
@mapio @jfinkels mapio I think it should check that cache_timeout is not None to allow for a…
… (I hope legale) value of 0 for such parameter.
11e249e
@yaph @jfinkels yaph Update master 8646625
@mitsuhiko @jfinkels mitsuhiko Don't build websites with travis 5b2faf3
@mitsuhiko @jfinkels mitsuhiko Removed padded JSON (JSONP) again.
The implementation was not clean and generally the needs for padded json
are disappearing now that all browsers support cross site communication
with the regular xmlhttprequest.
6fe0fd6
@mitsuhiko @jfinkels mitsuhiko Added #522 in modified version 5420957
@mattupstate @jfinkels mattupstate Fix documention for `after_this_request` fa616a3
@bev-a-tron @jfinkels bev-a-tron Fixes #519 by adding return statement 18134e7
@jfinkels jfinkels Merge branch 'master' into fix-issue-220
Conflicts:
	flask/helpers.py
5568bb6
@jfinkels jfinkels Added documentation explaining that the JSON encoder used in the json…
…ify function is from Flask.json_encoder_class.
989a7a7
@jfinkels jfinkels Merge branch 'fix-issue-220' of github.com:jfinkels/flask into fix-is…
…sue-220

Conflicts:
	flask/ctx.py
ee67d2e
@jfinkels jfinkels Merge branch 'master' of git://github.com/mitsuhiko/flask c25ba9a
@jfinkels jfinkels Merge branch 'master' into fix-issue-220 59d4c02

This pull request passes (merged 59d4c02 into 0ba758d).

Contributor

jfinkels commented Jun 25, 2012

I have merged with master.

Contributor

kennethreitz commented Jun 25, 2012

@mitsuhiko @rduplain thoughts?

Contributor

nvie commented Jul 16, 2012

A big +1 on this patch. Is there any progress on this?

+1 on this issue as well, but I think the patch is lacking. Wouldn't it make sense to have the custom JSON encoder apply when flask.json.dumps or the tojson template filter are called as well?

Ignore previous comment, this looks like it's been fixed in master in a cleaner way. The encoder attribute is now app.json_encoder. This ticket can be safely closed.

Contributor

rduplain commented Nov 14, 2012

Thanks for the comments.

rduplain closed this Nov 14, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment