Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
4f3c3a06
Commit
4f3c3a06
authored
Jan 19, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Drop trailing whitespace on indented JSON output. Closes #2429.
parent
dc18040b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
3 deletions
+31
-3
rest_framework/compat.py
+2
-0
rest_framework/renderers.py
+6
-2
tests/test_renderers.py
+23
-1
No files found.
rest_framework/compat.py
View file @
4f3c3a06
...
...
@@ -227,6 +227,8 @@ except ImportError:
if
six
.
PY3
:
SHORT_SEPARATORS
=
(
','
,
':'
)
LONG_SEPARATORS
=
(
', '
,
': '
)
INDENT_SEPARATORS
=
(
','
,
': '
)
else
:
SHORT_SEPARATORS
=
(
b
','
,
b
':'
)
LONG_SEPARATORS
=
(
b
', '
,
b
': '
)
INDENT_SEPARATORS
=
(
b
','
,
b
': '
)
rest_framework/renderers.py
View file @
4f3c3a06
...
...
@@ -18,7 +18,7 @@ from django.template import Context, RequestContext, loader, Template
from
django.test.client
import
encode_multipart
from
django.utils
import
six
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.settings
import
api_settings
from
rest_framework.request
import
is_form_media_type
,
override_method
...
...
@@ -87,7 +87,11 @@ class JSONRenderer(BaseRenderer):
renderer_context
=
renderer_context
or
{}
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
(
data
,
cls
=
self
.
encoder_class
,
...
...
tests/test_renderers.py
View file @
4f3c3a06
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.conf.urls
import
patterns
,
url
,
include
from
django.core.cache
import
cache
from
django.db
import
models
...
...
@@ -8,6 +7,7 @@ from django.test import TestCase
from
django.utils
import
six
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework
import
status
,
permissions
from
rest_framework.compat
import
OrderedDict
from
rest_framework.response
import
Response
from
rest_framework.views
import
APIView
from
rest_framework.renderers
import
BaseRenderer
,
JSONRenderer
,
BrowsableAPIRenderer
...
...
@@ -489,3 +489,25 @@ class CacheRenderTest(TestCase):
cached_resp
=
cache
.
get
(
self
.
cache_key
)
self
.
assertIsInstance
(
cached_resp
,
Response
)
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}'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment