encoding - data serialization¶
You can use encoding classes for loading and storing data in external systems. Currently supported formats are listed
in MimeTypes.
from kaiju_tools.encoding import SERIALIZERS
def my_loading_function(data, mime_type):
encoder = SERIALIZERS(mime_type)()
return encoder.loads(data)
You can also use Serializable to create serializable objects accepted by encoders
and implement custom serialization logic in its repr() method.
from kaiju_tools.encoding import Serializable
class MySerializableClass(Serializable):
def __init__(self, a, b):
self.a = a
self.b = b
def repr(self):
return {'a': self.a, 'b': self.b, 'c': 42}
- loads(self, buf)¶
Deserialize an object from JSON.
Parameters¶
- bufbytes-like or str
The message to decode.
Returns¶
- objAny
The deserialized object.
- dumps(self, obj)¶
Serialize an object to bytes.
Parameters¶
- objAny
The object to serialize.
Returns¶
- databytes
The serialized object.