Created
September 20, 2011 16:58
-
-
Save tomchristie/1229654 to your computer and use it in GitHub Desktop.
JSONRenderer that doesn't force ascii
This file contains 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
class UnicodeJSONRenderer(BaseRenderer): | |
""" | |
Renderer which serializes to JSON | |
""" | |
media_type = 'application/json' | |
format = 'json' | |
def render(self, obj=None, media_type=None): | |
""" | |
Renders *obj* into serialized JSON. | |
""" | |
if obj is None: | |
return '' | |
# If the media type looks like 'application/json; indent=4', then | |
# pretty print the result. | |
indent = get_media_type_params(media_type).get('indent', None) | |
sort_keys = False | |
try: | |
indent = max(min(int(indent), 8), 0) | |
sort_keys = True | |
except (ValueError, TypeError): | |
indent = None | |
return json.dumps(obj, cls=DateTimeAwareJSONEncoder, ensure_ascii=False, indent=indent, sort_keys=sort_keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment