mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
Merge 43546129ff
into c6326d0a67
This commit is contained in:
commit
d4f83e42c3
|
@ -13,7 +13,7 @@ from django.http.multipartparser import MultiPartParserError, parse_header, Chun
|
|||
from rest_framework.compat import etree, six, yaml, force_text
|
||||
from rest_framework.exceptions import ParseError
|
||||
from rest_framework import renderers
|
||||
import json
|
||||
# import json
|
||||
import datetime
|
||||
import decimal
|
||||
|
||||
|
@ -48,6 +48,7 @@ class JSONParser(BaseParser):
|
|||
|
||||
media_type = 'application/json'
|
||||
renderer_class = renderers.UnicodeJSONRenderer
|
||||
loads_function = 'json.loads'
|
||||
|
||||
def parse(self, stream, media_type=None, parser_context=None):
|
||||
"""
|
||||
|
@ -58,7 +59,12 @@ class JSONParser(BaseParser):
|
|||
|
||||
try:
|
||||
data = stream.read().decode(encoding)
|
||||
return json.loads(data)
|
||||
|
||||
components = self.loads_function.split('.')
|
||||
loads = __import__(components[0])
|
||||
for comp in components[1:]:
|
||||
loads = getattr(loads, comp)
|
||||
return loads(data)
|
||||
except ValueError as exc:
|
||||
raise ParseError('JSON parse error - %s' % six.text_type(exc))
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ REST framework also provides an HTML renderer the renders the browsable API.
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import copy
|
||||
import json
|
||||
# import json
|
||||
import django
|
||||
from django import forms
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
@ -55,6 +55,7 @@ class JSONRenderer(BaseRenderer):
|
|||
encoder_class = encoders.JSONEncoder
|
||||
ensure_ascii = True
|
||||
charset = None
|
||||
dumps_function = 'json.dumps'
|
||||
# JSON is a binary encoding, that can be encoded as utf-8, utf-16 or utf-32.
|
||||
# See: http://www.ietf.org/rfc/rfc4627.txt
|
||||
# Also: http://lucumr.pocoo.org/2013/7/19/application-mimetypes-and-encodings/
|
||||
|
@ -81,7 +82,12 @@ class JSONRenderer(BaseRenderer):
|
|||
except (ValueError, TypeError):
|
||||
indent = None
|
||||
|
||||
ret = json.dumps(data, cls=self.encoder_class,
|
||||
components = self.dumps_function.split('.')
|
||||
dumps = __import__(components[0])
|
||||
for comp in components[1:]:
|
||||
dumps = getattr(dumps, comp)
|
||||
|
||||
ret = dumps(data, cls=self.encoder_class,
|
||||
indent=indent, ensure_ascii=self.ensure_ascii)
|
||||
|
||||
# On python 2.x json.dumps() returns bytestrings if ensure_ascii=True,
|
||||
|
|
Loading…
Reference in New Issue
Block a user