/usr/lib/python3/dist-packages/restless/serializers.py is in python3-restless 2.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | from .exceptions import BadRequest
from .utils import json, MoreTypesJSONEncoder
class Serializer(object):
"""
A base serialization class.
Defines the protocol expected of a serializer, but only raises
``NotImplementedError``.
Either subclass this or provide an object with the same
``deserialize/serialize`` methods on it.
"""
def deserialize(self, body):
"""
Handles deserializing data coming from the user.
Should return a plain Python data type (such as a dict or list)
containing the data.
:param body: The body of the current request
:type body: string
:returns: The deserialized data
:rtype: ``list`` or ``dict``
"""
raise NotImplementedError("Subclasses must implement this method.")
def serialize(self, data):
"""
Handles serializing data being sent to the user.
Should return a plain Python string containing the serialized data
in the appropriate format.
:param data: The body for the response
:type data: ``list`` or ``dict``
:returns: A serialized version of the data
:rtype: string
"""
raise NotImplementedError("Subclasses must implement this method.")
class JSONSerializer(Serializer):
def deserialize(self, body):
"""
The low-level deserialization.
Underpins ``deserialize``, ``deserialize_list`` &
``deserialize_detail``.
Has no built-in smarts, simply loads the JSON.
:param body: The body of the current request
:type body: string
:returns: The deserialized data
:rtype: ``list`` or ``dict``
"""
try:
if isinstance(body, bytes):
return json.loads(body.decode('utf-8'))
return json.loads(body)
except ValueError:
raise BadRequest('Request body is not valid JSON')
def serialize(self, data):
"""
The low-level serialization.
Underpins ``serialize``, ``serialize_list`` &
``serialize_detail``.
Has no built-in smarts, simply dumps the JSON.
:param data: The body for the response
:type data: string
:returns: A serialized version of the data
:rtype: string
"""
return json.dumps(data, cls=MoreTypesJSONEncoder)
|