Skip to content

Instantly share code, notes, and snippets.

@tomchristie
Created September 20, 2011 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomchristie/1229654 to your computer and use it in GitHub Desktop.
Save tomchristie/1229654 to your computer and use it in GitHub Desktop.
JSONRenderer that doesn't force ascii
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