mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Drop trailing whitespace on indented JSON output. Closes #2429.
This commit is contained in:
parent
dc18040ba4
commit
4f3c3a06cf
|
@ -227,6 +227,8 @@ except ImportError:
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
SHORT_SEPARATORS = (',', ':')
|
SHORT_SEPARATORS = (',', ':')
|
||||||
LONG_SEPARATORS = (', ', ': ')
|
LONG_SEPARATORS = (', ', ': ')
|
||||||
|
INDENT_SEPARATORS = (',', ': ')
|
||||||
else:
|
else:
|
||||||
SHORT_SEPARATORS = (b',', b':')
|
SHORT_SEPARATORS = (b',', b':')
|
||||||
LONG_SEPARATORS = (b', ', b': ')
|
LONG_SEPARATORS = (b', ', b': ')
|
||||||
|
INDENT_SEPARATORS = (b',', b': ')
|
||||||
|
|
|
@ -18,7 +18,7 @@ from django.template import Context, RequestContext, loader, Template
|
||||||
from django.test.client import encode_multipart
|
from django.test.client import encode_multipart
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from rest_framework import exceptions, serializers, status, VERSION
|
from rest_framework import exceptions, serializers, status, VERSION
|
||||||
from rest_framework.compat import SHORT_SEPARATORS, LONG_SEPARATORS
|
from rest_framework.compat import SHORT_SEPARATORS, LONG_SEPARATORS, INDENT_SEPARATORS
|
||||||
from rest_framework.exceptions import ParseError
|
from rest_framework.exceptions import ParseError
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.request import is_form_media_type, override_method
|
from rest_framework.request import is_form_media_type, override_method
|
||||||
|
@ -87,7 +87,11 @@ class JSONRenderer(BaseRenderer):
|
||||||
|
|
||||||
renderer_context = renderer_context or {}
|
renderer_context = renderer_context or {}
|
||||||
indent = self.get_indent(accepted_media_type, renderer_context)
|
indent = self.get_indent(accepted_media_type, renderer_context)
|
||||||
separators = SHORT_SEPARATORS if (indent is None and self.compact) else LONG_SEPARATORS
|
|
||||||
|
if indent is None:
|
||||||
|
separators = SHORT_SEPARATORS if self.compact else LONG_SEPARATORS
|
||||||
|
else:
|
||||||
|
separators = INDENT_SEPARATORS
|
||||||
|
|
||||||
ret = json.dumps(
|
ret = json.dumps(
|
||||||
data, cls=self.encoder_class,
|
data, cls=self.encoder_class,
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from django.conf.urls import patterns, url, include
|
from django.conf.urls import patterns, url, include
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
@ -8,6 +7,7 @@ from django.test import TestCase
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from rest_framework import status, permissions
|
from rest_framework import status, permissions
|
||||||
|
from rest_framework.compat import OrderedDict
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.renderers import BaseRenderer, JSONRenderer, BrowsableAPIRenderer
|
from rest_framework.renderers import BaseRenderer, JSONRenderer, BrowsableAPIRenderer
|
||||||
|
@ -489,3 +489,25 @@ class CacheRenderTest(TestCase):
|
||||||
cached_resp = cache.get(self.cache_key)
|
cached_resp = cache.get(self.cache_key)
|
||||||
self.assertIsInstance(cached_resp, Response)
|
self.assertIsInstance(cached_resp, Response)
|
||||||
self.assertEqual(cached_resp.content, resp.content)
|
self.assertEqual(cached_resp.content, resp.content)
|
||||||
|
|
||||||
|
|
||||||
|
class TestJSONIndentationStyles:
|
||||||
|
def test_indented(self):
|
||||||
|
renderer = JSONRenderer()
|
||||||
|
data = OrderedDict([('a', 1), ('b', 2)])
|
||||||
|
assert renderer.render(data) == b'{"a":1,"b":2}'
|
||||||
|
|
||||||
|
def test_compact(self):
|
||||||
|
renderer = JSONRenderer()
|
||||||
|
data = OrderedDict([('a', 1), ('b', 2)])
|
||||||
|
context = {'indent': 4}
|
||||||
|
assert (
|
||||||
|
renderer.render(data, renderer_context=context) ==
|
||||||
|
b'{\n "a": 1,\n "b": 2\n}'
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_long_form(self):
|
||||||
|
renderer = JSONRenderer()
|
||||||
|
renderer.compact = False
|
||||||
|
data = OrderedDict([('a', 1), ('b', 2)])
|
||||||
|
assert renderer.render(data) == b'{"a": 1, "b": 2}'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user