├── .gitignore ├── README.md ├── setup.py └── ujson_drf ├── __init__.py ├── parsers.py └── renderers.py /.gitignore: -------------------------------------------------------------------------------- 1 | led / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *,cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # dotenv 80 | .env 81 | 82 | # virtualenv 83 | .venv/ 84 | venv/ 85 | ENV/ 86 | 87 | # Spyder project settings 88 | .spyderproject 89 | 90 | # Rope project settings 91 | .ropeproject 92 | 93 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ujson_drf 2 | JSON Renderer and Parser for Django Rest Framework using the ultra fast json (in C). 3 | 4 | ## Install using 5 | `pip install ujson_drf` 6 | 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup(name='ujson_drf', 4 | version='0.2', 5 | description='JSON Renderer and Parser for Django Rest Framework using the ultra fast json (in C).', 6 | url='https://github.com/akshaysharma096/ujson_drf', 7 | author='Akshay Sharma', 8 | author_email='mailbag.akshay@gmail.com', 9 | license='MIT', 10 | packages=['ujson_drf'], 11 | install_requires=[ 12 | 'ujson', 13 | 'django', 14 | 'djangorestframework' 15 | ], 16 | classifiers=['Framework :: Django', 'Programming Language :: Python', 17 | 'Intended Audience :: Developers', 'Topic :: Software Development :: Libraries :: Python Modules'], 18 | keywords='django djangorestframework restframework djangorest parser renderer', 19 | zip_safe=False) 20 | -------------------------------------------------------------------------------- /ujson_drf/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akshaysharma096/ujson_drf/b90113e50caa1214109d6fee5c54783c1fd73b31/ujson_drf/__init__.py -------------------------------------------------------------------------------- /ujson_drf/parsers.py: -------------------------------------------------------------------------------- 1 | from rest_framework.parsers import JSONParser 2 | import ujson 3 | from django.conf import settings 4 | from django.utils import six 5 | 6 | 7 | class UJSONParser(JSONParser): 8 | 9 | def parse(self, stream, media_type=None, parser_context=None): 10 | parser_context = parser_context or {} 11 | encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET) 12 | try: 13 | data = stream.read().decode(encoding) 14 | return ujson.loads(data) 15 | except ValueError as e: 16 | raise ParseError('JSON parse error - %s' % six.text_type(exc)) 17 | -------------------------------------------------------------------------------- /ujson_drf/renderers.py: -------------------------------------------------------------------------------- 1 | import ujson 2 | from rest_framework import VERSION, exceptions, serializers, status 3 | from rest_framework.compat import ( 4 | INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS, coreapi, 5 | template_render 6 | ) 7 | from django.conf import settings 8 | from rest_framework.renderers import JSONRenderer 9 | from collections import defaultdict 10 | 11 | 12 | class USJONRenderer(JSONRenderer): 13 | ensure_ascii = False 14 | double_precision = 3 15 | escape_forward_slashes = False 16 | 17 | def render(self, data, accepted_media_type=None, renderer_context=None): 18 | """ 19 | Render `data` into JSON, returning a bytestring. 20 | """ 21 | if data is None: 22 | return bytes() 23 | renderer_context = renderer_context or {} 24 | indent = self.get_indent(accepted_media_type, renderer_context) 25 | 26 | if indent is None: 27 | separators = SHORT_SEPARATORS if self.compact else LONG_SEPARATORS 28 | else: 29 | separators = INDENT_SEPARATORS 30 | 31 | ret = ujson.dumps(data, ensure_ascii=self.ensure_ascii, 32 | double_precision=self.double_precision, escape_forward_slashes=self.escape_forward_slashes) 33 | # On python 2.x json.dumps() returns bytestrings if ensure_ascii=True, 34 | # but if ensure_ascii=False, the return type is underspecified, 35 | # and may (or may not) be unicode. 36 | # On python 3.x json.dumps() returns unicode strings. 37 | if isinstance(ret, six.text_type): 38 | # We always fully escape \u2028 and \u2029 to ensure we output JSON 39 | # that is a strict javascript subset. If bytes were returned 40 | # by json.dumps() then we don't have these characters in any case. 41 | # See: http://timelessrepo.com/json-isnt-a-javascript-subset 42 | ret = ret.replace('\u2028', '\\u2028').replace('\u2029', '\\u2029') 43 | return bytes(ret.encode('utf-8')) 44 | return ret 45 | --------------------------------------------------------------------------------