Hello,
I've been struggling for a couple hours to override the default JSONEncoder. I started by trying the Flask method of setting Flask.json_encoder. After stepping through the code, it looks like Flask-Restful discards that setting altogether.
I also tried using @api.representation('application/json') with the example from the "Response Formats" section of the docs. Within the function body, I called the standard json.dumps with a custom JSONEncoder-derived class. That resulted in a recursive loop that I didn't dig much deeper into.
I'd prefer to avoid using output parameters since what I'm really trying to do is blindly pass data from database back to the caller. Any ideas here?
-Scott
I was able to solve this with a night of sleep and the following two lines:
import flask.ext.restful.representations.json
flask.ext.restful.representations.json.settings["cls"] = MyCustomJSONEncoder
It would be nice if flask-restful was updated to support the Flask.json_encoder member, but more of a nice-to-have than a bug :)
I would love to see this in the documentation - rendering data types like datetimes is a really common need. It took me a long time to find this solution.
+1, same thing. Please respect flask's json_encoder
See the reasons in #224 for this.
also related: #101, #207, #237
opened #399 to update the docs with an explanation.
+1 I lost hours trying to figure out why the custom encoder I installed on Flask wasn't getting used. If you're going to provide an override point, that's one thing, but if you're going to override by default, maybe add some sort of warning if there's a non-standard current_app.json_encoder set, so that people don't have to trace through a bunch of code to understand why their custom encoder isn't being used.
If you're using the latest version (which should include the JSON configuration), you could do this:
class MyConfig(object):
RESTFUL_JSON = {'cls': MyCustomEncoder}
Or if you want to make sure your RESTful encoder and the Flask encoder stay synchronized:
class MyConfig(object):
RESTFUL_JSON = {...} # add whatever settings here
@staticmethod
def init_app(app):
app.config['RESTFUL_JSON']['cls'] = app.json_encoder = MyCustomEncoder
And "initialize" your configuration just like an extension after the app's created:
app = Flask(__name__)
app.config.from_object(MyConfig)
MyConfig.init_app(app)
There should probably be something in the docs about using the RESTFUL_JSON configuration setting, which was an oversight on my part when I added it. @joshfriend Where do you think the best place to add this is at?
@justanr somewhere near the note I added in 1cb517f7 should be fine (extending.rst).
Most helpful comment
If you're using the latest version (which should include the JSON configuration), you could do this:
Or if you want to make sure your RESTful encoder and the Flask encoder stay synchronized:
And "initialize" your configuration just like an extension after the app's created:
There should probably be something in the docs about using the RESTFUL_JSON configuration setting, which was an oversight on my part when I added it. @joshfriend Where do you think the best place to add this is at?