-
Notifications
You must be signed in to change notification settings - Fork 27
More ujson options #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,46 @@ | ||
| from __future__ import unicode_literals | ||
| from rest_framework.compat import six | ||
| from rest_framework.renderers import BaseRenderer | ||
| from rest_framework.renderers import JSONRenderer | ||
| import ujson | ||
|
|
||
|
|
||
| class UJSONRenderer(BaseRenderer): | ||
| class UJSONRenderer(JSONRenderer): | ||
| """ | ||
| Renderer which serializes to JSON. | ||
| Applies JSON's backslash-u character escaping for non-ascii characters. | ||
| Uses the blazing-fast ujson library for serialization. | ||
| """ | ||
|
|
||
| media_type = 'application/json' | ||
| format = 'json' | ||
| ensure_ascii = True | ||
| charset = None | ||
|
|
||
| def render(self, data, *args, **kwargs): | ||
| # Controls how many decimals to encode for double or decimal values. | ||
| double_precision = 9 | ||
| # Controls whether forward slashes (/) are escaped. | ||
| escape_forward_slashes = False | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ujson default is |
||
| # Used to enable special encoding of "unsafe" HTML characters into safer | ||
| # Unicode sequences. | ||
| encode_html_chars = False | ||
|
|
||
| def render(self, data, accepted_media_type=None, renderer_context=None): | ||
| if data is None: | ||
| return bytes() | ||
|
|
||
| ret = ujson.dumps(data, ensure_ascii=self.ensure_ascii) | ||
| renderer_context = renderer_context or {} | ||
| indent = self.get_indent(accepted_media_type, renderer_context) | ||
|
|
||
| ret = ujson.dumps( | ||
| data, | ||
| ensure_ascii=self.ensure_ascii, | ||
| escape_forward_slashes=self.escape_forward_slashes, | ||
| encode_html_chars=self.encode_html_chars, | ||
| double_precision=self.double_precision, | ||
| indent=indent or 0, | ||
| ) | ||
|
|
||
| # force return value to unicode | ||
| if isinstance(ret, six.text_type): | ||
| # We always fully escape \u2028 and \u2029 to ensure we output JSON | ||
| # that is a strict javascript subset. If bytes were returned | ||
| # by json.dumps() then we don't have these characters in any case. | ||
| # See: http://timelessrepo.com/json-isnt-a-javascript-subset | ||
| ret = ret.replace('\u2028', '\\u2028').replace('\u2029', '\\u2029') | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copying from |
||
| return bytes(ret.encode('utf-8')) | ||
| return ret | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,5 +10,5 @@ | |
| author_email='[email protected]', | ||
| url='https://github.com/gizmag/drf-ujson-renderer', | ||
| packages=find_packages(), | ||
| install_requires=['django', 'ujson', 'djangorestframework'] | ||
| install_requires=['django', 'ujson>=1.35', 'djangorestframework'] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This version has |
||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extending to get
get_indent.