Serializers
Serializer (ABC, Generic)
Base interface to inherit for all the serializers
Source code in melange/serializers/interfaces.py
class Serializer(ABC, Generic[T]):
"""
Base interface to inherit for all the serializers
"""
@staticmethod
@abstractmethod
def identifier() -> int:
raise NotImplementedError
def manifest(self, data: T) -> Optional[str]:
"""
Given an object which represents some data, return the
manifest of that object (if any)
Args:
data:
Returns:
The manifest of the object
"""
return None
def deserialize(self, data: str, manifest: Optional[str] = None) -> T:
"""
Deserializes a data string. The manifest helps the serializer by
providing information on how this object must be deserialized.
Args:
data: the data string to deserialize
manifest: the manifest that corresponds to the serialized string
Returns:
The data object
"""
pass
def serialize(self, data: T) -> str:
"""
Serializes and object to a string representation
Args:
data: the data object to serialize
Returns:
A string representation of the `data` object made with this serializer
"""
pass
deserialize(self, data, manifest=None)
Deserializes a data string. The manifest helps the serializer by providing information on how this object must be deserialized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str |
the data string to deserialize |
required |
manifest |
Optional[str] |
the manifest that corresponds to the serialized string |
None |
Returns:
Type | Description |
---|---|
~T |
The data object |
Source code in melange/serializers/interfaces.py
def deserialize(self, data: str, manifest: Optional[str] = None) -> T:
"""
Deserializes a data string. The manifest helps the serializer by
providing information on how this object must be deserialized.
Args:
data: the data string to deserialize
manifest: the manifest that corresponds to the serialized string
Returns:
The data object
"""
pass
manifest(self, data)
Given an object which represents some data, return the manifest of that object (if any)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
~T |
required |
Returns:
Type | Description |
---|---|
Optional[str] |
The manifest of the object |
serialize(self, data)
Serializes and object to a string representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
~T |
the data object to serialize |
required |
Returns:
Type | Description |
---|---|
str |
A string representation of the |
JsonSerializer (Serializer)
Serializes and deserializes python dictionaries in json format
Source code in melange/serializers/json.py
class JsonSerializer(Serializer[Dict]):
"""
Serializes and deserializes python dictionaries in json format
"""
@staticmethod
def identifier() -> int:
return 1
def manifest(self, data: Dict) -> str:
return "json"
def deserialize(self, serialized_data: str, manifest: Optional[str] = None) -> Dict:
data = json.loads(serialized_data)
return data
def serialize(self, data: Dict) -> str:
return json.dumps(data)
deserialize(self, serialized_data, manifest=None)
Deserializes a data string. The manifest helps the serializer by providing information on how this object must be deserialized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
the data string to deserialize |
required | |
manifest |
Optional[str] |
the manifest that corresponds to the serialized string |
None |
Returns:
Type | Description |
---|---|
Dict |
The data object |
manifest(self, data)
Given an object which represents some data, return the manifest of that object (if any)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict |
required |
Returns:
Type | Description |
---|---|
str |
The manifest of the object |
serialize(self, data)
Serializes and object to a string representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict |
the data object to serialize |
required |
Returns:
Type | Description |
---|---|
str |
A string representation of the |
PickleSerializer (Serializer)
Serializes DomainEvents with pickle.
Source code in melange/serializers/pickle.py
class PickleSerializer(Serializer[DomainEvent]):
"""
Serializes DomainEvents with pickle.
"""
@staticmethod
def identifier() -> int:
return 2
def manifest(self, data: DomainEvent) -> str:
return data.__class__.__qualname__
def deserialize(self, data: str, manifest: Optional[str] = None) -> DomainEvent:
return pickle.loads(codecs.decode(data.encode(), "base64"))
def serialize(self, data: DomainEvent) -> str:
return codecs.encode(pickle.dumps(data), "base64").decode()
deserialize(self, data, manifest=None)
Deserializes a data string. The manifest helps the serializer by providing information on how this object must be deserialized.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str |
the data string to deserialize |
required |
manifest |
Optional[str] |
the manifest that corresponds to the serialized string |
None |
Returns:
Type | Description |
---|---|
DomainEvent |
The data object |
manifest(self, data)
Given an object which represents some data, return the manifest of that object (if any)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DomainEvent |
required |
Returns:
Type | Description |
---|---|
str |
The manifest of the object |
serialize(self, data)
Serializes and object to a string representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
DomainEvent |
the data object to serialize |
required |
Returns:
Type | Description |
---|---|
str |
A string representation of the |