-
Notifications
You must be signed in to change notification settings - Fork 19
/
json_serializer.py
43 lines (36 loc) · 1.17 KB
/
json_serializer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
from .interface import ContentTypeSerializer
from ..exceptions import SerializationException
from ..compat import string_type
class JsonSerializer(ContentTypeSerializer):
content_type = ["application/json"]
@staticmethod
def dump(data):
"""
should return back a bytes (or string in python 2),
representation of your object, to be used in e.g. response
bodies.
"""
return json.dumps(data).encode("UTF-8")
@property
def main_type(self):
return self.content_type[0]
@staticmethod
def load(raw_bytes):
"""
given a bytes object, should return a base python data
structure that represents the object.
"""
try:
if not isinstance(raw_bytes, string_type):
raw_bytes = raw_bytes.decode()
return json.loads(raw_bytes)
except ValueError as e:
raise SerializationException(str(e))
@staticmethod
def can_handle(content_type_name):
"""
given a content type, returns true if this serializer
can convert bodies of the given type.
"""
return "json" in content_type_name