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
09c53bba
Commit
09c53bba
authored
Aug 11, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor JSONRenderer slightly for easier overriding
parent
c6326d0a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
16 deletions
+21
-16
rest_framework/renderers.py
+21
-16
No files found.
rest_framework/renderers.py
View file @
09c53bba
...
@@ -54,32 +54,37 @@ class JSONRenderer(BaseRenderer):
...
@@ -54,32 +54,37 @@ class JSONRenderer(BaseRenderer):
format
=
'json'
format
=
'json'
encoder_class
=
encoders
.
JSONEncoder
encoder_class
=
encoders
.
JSONEncoder
ensure_ascii
=
True
ensure_ascii
=
True
charset
=
None
# JSON is a binary encoding, that can be encoded as utf-8, utf-16 or utf-32.
# We don't set a charset because 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
# See: http://www.ietf.org/rfc/rfc4627.txt
# Also: http://lucumr.pocoo.org/2013/7/19/application-mimetypes-and-encodings/
# Also: http://lucumr.pocoo.org/2013/7/19/application-mimetypes-and-encodings/
charset
=
None
def
get_indent
(
self
,
accepted_media_type
,
renderer_context
):
if
accepted_media_type
:
# If the media type looks like 'application/json; indent=4',
# then pretty print the result.
base_media_type
,
params
=
parse_header
(
accepted_media_type
.
encode
(
'ascii'
))
try
:
return
max
(
min
(
int
(
params
[
'indent'
]),
8
),
0
)
except
(
KeyError
,
ValueError
,
TypeError
):
pass
# If 'indent' is provided in the context, then pretty print the result.
# E.g. If we're being called by the BrowsableAPIRenderer.
return
renderer_context
.
get
(
'indent'
,
None
)
def
render
(
self
,
data
,
accepted_media_type
=
None
,
renderer_context
=
None
):
def
render
(
self
,
data
,
accepted_media_type
=
None
,
renderer_context
=
None
):
"""
"""
Render `data` into JSON.
Render `data` into JSON
, returning a bytestring
.
"""
"""
if
data
is
None
:
if
data
is
None
:
return
bytes
()
return
bytes
()
# If 'indent' is provided in the context, then pretty print the result.
# E.g. If we're being called by the BrowsableAPIRenderer.
renderer_context
=
renderer_context
or
{}
renderer_context
=
renderer_context
or
{}
indent
=
renderer_context
.
get
(
'indent'
,
None
)
indent
=
self
.
get_indent
(
accepted_media_type
,
renderer_context
)
if
accepted_media_type
:
# If the media type looks like 'application/json; indent=4',
# then pretty print the result.
base_media_type
,
params
=
parse_header
(
accepted_media_type
.
encode
(
'ascii'
))
indent
=
params
.
get
(
'indent'
,
indent
)
try
:
indent
=
max
(
min
(
int
(
indent
),
8
),
0
)
except
(
ValueError
,
TypeError
):
indent
=
None
ret
=
json
.
dumps
(
data
,
cls
=
self
.
encoder_class
,
ret
=
json
.
dumps
(
data
,
cls
=
self
.
encoder_class
,
indent
=
indent
,
ensure_ascii
=
self
.
ensure_ascii
)
indent
=
indent
,
ensure_ascii
=
self
.
ensure_ascii
)
...
...
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