Last active
January 4, 2016 01:24
-
-
Save asluchevskiy/4270dbbc63377dc86165 to your computer and use it in GitHub Desktop.
Makes flask-restful json API unicode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from flask_restful import Api, output_json | |
import json | |
import datetime | |
class DateTimeJSONEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, datetime.datetime): | |
return obj.isoformat() | |
else: | |
return super(DateTimeJSONEncoder, self).default(obj) | |
class UnicodeApi(Api): | |
def __init__(self, *args, **kwargs): | |
super(UnicodeApi, self).__init__(*args, **kwargs) | |
self.app.config['RESTFUL_JSON'] = { | |
'ensure_ascii': False, | |
'cls': DateTimeJSONEncoder, | |
} | |
self.representations = { | |
'application/json; charset=utf-8': output_json, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment