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
de4ef6e3
Commit
de4ef6e3
authored
Dec 05, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2195 from tomchristie/tomchristie-escape-u2028-u2029-json
Escape \u2028 and \u2029 in JSON output.
parents
e2b39088
23fa6e54
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
0 deletions
+14
-0
rest_framework/renderers.py
+5
-0
tests/test_renderers.py
+9
-0
No files found.
rest_framework/renderers.py
View file @
de4ef6e3
...
@@ -102,6 +102,11 @@ class JSONRenderer(BaseRenderer):
...
@@ -102,6 +102,11 @@ class JSONRenderer(BaseRenderer):
# and may (or may not) be unicode.
# and may (or may not) be unicode.
# On python 3.x json.dumps() returns unicode strings.
# On python 3.x json.dumps() returns unicode strings.
if
isinstance
(
ret
,
six
.
text_type
):
if
isinstance
(
ret
,
six
.
text_type
):
# We always fully escape \u2028 and \u2029 to ensure we output JSON
# that is a strict javascript subset. If bytes were returned
# by json.dumps() then we don't have these characters in any case.
# See: http://timelessrepo.com/json-isnt-a-javascript-subset
ret
=
ret
.
replace
(
'
\u2028
'
,
'
\\
u2028'
)
.
replace
(
'
\u2029
'
,
'
\\
u2029'
)
return
bytes
(
ret
.
encode
(
'utf-8'
))
return
bytes
(
ret
.
encode
(
'utf-8'
))
return
ret
return
ret
...
...
tests/test_renderers.py
View file @
de4ef6e3
...
@@ -384,6 +384,15 @@ class UnicodeJSONRendererTests(TestCase):
...
@@ -384,6 +384,15 @@ class UnicodeJSONRendererTests(TestCase):
content
=
renderer
.
render
(
obj
,
'application/json'
)
content
=
renderer
.
render
(
obj
,
'application/json'
)
self
.
assertEqual
(
content
,
'{"countries":["United Kingdom","France","España"]}'
.
encode
(
'utf-8'
))
self
.
assertEqual
(
content
,
'{"countries":["United Kingdom","France","España"]}'
.
encode
(
'utf-8'
))
def
test_u2028_u2029
(
self
):
# The \u2028 and \u2029 characters should be escaped,
# even when the non-escaping unicode representation is used.
# Regression test for #2169
obj
=
{
'should_escape'
:
'
\u2028\u2029
'
}
renderer
=
JSONRenderer
()
content
=
renderer
.
render
(
obj
,
'application/json'
)
self
.
assertEqual
(
content
,
'{"should_escape":"
\\
u2028
\\
u2029"}'
.
encode
(
'utf-8'
))
class
AsciiJSONRendererTests
(
TestCase
):
class
AsciiJSONRendererTests
(
TestCase
):
"""
"""
...
...
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