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
22af49bf
Commit
22af49bf
authored
Sep 12, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tidy up JSONEncoder
parent
5e39e159
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
33 deletions
+34
-33
rest_framework/utils/encoders.py
+34
-33
No files found.
rest_framework/utils/encoders.py
View file @
22af49bf
...
...
@@ -7,7 +7,6 @@ from django.db.models.query import QuerySet
from
django.utils.datastructures
import
SortedDict
from
django.utils.functional
import
Promise
from
rest_framework.compat
import
force_text
# from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata
import
datetime
import
decimal
import
types
...
...
@@ -17,45 +16,47 @@ import json
class
JSONEncoder
(
json
.
JSONEncoder
):
"""
JSONEncoder subclass that knows how to encode date/time/timedelta,
decimal types,
and generator
s.
decimal types,
generators and other basic python object
s.
"""
def
default
(
self
,
o
):
def
default
(
self
,
o
bj
):
# For Date Time string spec, see ECMA 262
# http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
if
isinstance
(
o
,
Promise
):
return
force_text
(
o
)
elif
isinstance
(
o
,
datetime
.
datetime
):
r
=
o
.
isoformat
()
if
o
.
microsecond
:
r
=
r
[:
23
]
+
r
[
26
:]
if
r
.
endswith
(
'+00:00'
):
r
=
r
[:
-
6
]
+
'Z'
return
r
elif
isinstance
(
o
,
datetime
.
date
):
return
o
.
isoformat
()
elif
isinstance
(
o
,
datetime
.
time
):
if
timezone
and
timezone
.
is_aware
(
o
):
if
isinstance
(
o
bj
,
Promise
):
return
force_text
(
o
bj
)
elif
isinstance
(
o
bj
,
datetime
.
datetime
):
r
epresentation
=
obj
.
isoformat
()
if
o
bj
.
microsecond
:
r
epresentation
=
representation
[:
23
]
+
representation
[
26
:]
if
r
epresentation
.
endswith
(
'+00:00'
):
r
epresentation
=
representation
[:
-
6
]
+
'Z'
return
r
epresentation
elif
isinstance
(
o
bj
,
datetime
.
date
):
return
o
bj
.
isoformat
()
elif
isinstance
(
o
bj
,
datetime
.
time
):
if
timezone
and
timezone
.
is_aware
(
o
bj
):
raise
ValueError
(
"JSON can't represent timezone-aware times."
)
r
=
o
.
isoformat
()
if
o
.
microsecond
:
r
=
r
[:
12
]
return
r
elif
isinstance
(
o
,
datetime
.
timedelta
):
return
str
(
o
.
total_seconds
())
elif
isinstance
(
o
,
decimal
.
Decimal
):
return
float
(
o
)
elif
isinstance
(
o
,
QuerySet
):
return
list
(
o
)
elif
hasattr
(
o
,
'tolist'
):
return
o
.
tolist
()
elif
hasattr
(
o
,
'__getitem__'
):
representation
=
obj
.
isoformat
()
if
obj
.
microsecond
:
representation
=
representation
[:
12
]
return
representation
elif
isinstance
(
obj
,
datetime
.
timedelta
):
return
str
(
obj
.
total_seconds
())
elif
isinstance
(
obj
,
decimal
.
Decimal
):
# Serializers will coerce decimals to strings by default.
return
float
(
obj
)
elif
isinstance
(
obj
,
QuerySet
):
return
list
(
obj
)
elif
hasattr
(
obj
,
'tolist'
):
# Numpy arrays and array scalars.
return
obj
.
tolist
()
elif
hasattr
(
obj
,
'__getitem__'
):
try
:
return
dict
(
o
)
return
dict
(
o
bj
)
except
:
pass
elif
hasattr
(
o
,
'__iter__'
):
return
[
i
for
i
in
o
]
return
super
(
JSONEncoder
,
self
)
.
default
(
o
)
elif
hasattr
(
o
bj
,
'__iter__'
):
return
[
i
tem
for
item
in
obj
]
return
super
(
JSONEncoder
,
self
)
.
default
(
o
bj
)
try
:
...
...
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