Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pikatasks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
django
pikatasks
Commits
87bceac1
Commit
87bceac1
authored
6 years ago
by
Art
Browse files
Options
Downloads
Patches
Plain Diff
Add bytes support to serialization
parent
312eaf47
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pikatasks/serialization.py
+11
-2
11 additions, 2 deletions
pikatasks/serialization.py
with
11 additions
and
2 deletions
pikatasks/serialization.py
+
11
−
2
View file @
87bceac1
...
...
@@ -3,6 +3,7 @@ import json
import
datetime
import
collections
import
itertools
import
base64
from
.
import
settings
from
.utils
import
logger
...
...
@@ -67,11 +68,17 @@ def json_serialize_tweaks(obj):
JSON_PYTHON_DATA_TYPE
:
"
set
"
,
JSON_PYTHON_DATA_VALUE
:
list
(
obj
),
}
elif
isinstance
(
obj
,
bytes
):
logger
.
warning
(
"
Going to serialize: {obj}
"
.
format
(
obj
=
obj
))
return
{
JSON_PYTHON_DATA_TYPE
:
"
bytes/base64
"
,
JSON_PYTHON_DATA_VALUE
:
base64
.
b64encode
(
obj
).
decode
(
"
utf-8
"
)
# need to .decode because b64encode returns bytes
}
elif
isinstance
(
obj
,
collections
.
Iterable
):
#
iterators and
other iterables will become lists
# other iterable
s and iterator
s will become lists
elements
=
list
(
itertools
.
islice
(
obj
,
JSON_ITER_MAX_YIELD
))
# protect from trolls with itertools.repeat()
if
len
(
elements
is
JSON_ITER_MAX_YIELD
):
logger
.
warning
(
"
Will not automatically yield more than {n} elements from {
obj
}.
"
.
format
(
n
=
JSON_ITER_MAX_YIELD
,
obj
=
obj
))
logger
.
warning
(
"
Will not automatically yield more than {n} elements from {
t
}.
"
.
format
(
n
=
JSON_ITER_MAX_YIELD
,
t
=
type
(
obj
))
)
return
elements
elif
isinstance
(
obj
,
datetime
.
datetime
):
return
{
...
...
@@ -101,6 +108,8 @@ def json_deserialize_tweaks(obj):
value
=
obj
[
JSON_PYTHON_DATA_VALUE
]
if
type_name
==
"
set
"
:
return
set
(
value
)
elif
type_name
==
"
bytes/base64
"
:
return
base64
.
b64decode
(
value
.
encode
(
"
utf-8
"
))
elif
type_name
==
"
datetime
"
:
return
str_to_datetime
(
value
)
elif
type_name
==
"
date
"
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment