Create IEventSerializer and EventSerializerRegistry
Description
The island and agent cannot have a priori knowledge about all event types. Rather, events will need to be registered with the island so that it can serialize/deserialize them. To achieve this, we'll introduce IEventSerializer and EventSerializerRegistry.
IEventSerializer
Each subclass of AbstractEvent must have a corresponding IEventSerializer that provides a serialize() and deserialize() method.
from typing import Union, Dict, List, Type
# See https://www.oreilly.com/library/view/mastering-object-oriented-python/9781789531367/13d1b3d7-12ef-4d21-bd0b-1d45c3ac1ad8.xhtml
# See also https://stackoverflow.com/questions/51291722/define-a-jsonable-type-using-mypy-pep-526
JSONSerializable = Union[Dict[str, 'JSON'], List['JSON'], int, str, float, bool, Type[None]]
OR
JSONSerializable = Union[Dict[str, Any], List[Any], int, str, float, bool, Type[None]]
class IEventSerializer:
def serialize(self, event: AbstractEvent) -> JSONSerializable:
pass
def deserialize(self, serialized_event: JSONSerializable) -> AbstractEvent:
pass
NOTE: We'll use Dict, not Mapping for the type hint. It turns out that Mappings are not JSON serializable, while Dicts are.
EventSerializerRegistry
The EventSerializerRegistry should accept a key that is a string equal to the event's class name and return an IEventSerializer capable of serializing/deserializing the corresponding event type. This can be implemented using register() and get() methods, or as a Mapping by implementing __getitem__().
Tasks
- [ ] Define
IEventSerializer - [ ] Implement
EventSerializerRegistry - [ ] Implement an
IEventSerializerforCredentialsStolenEvent - [ ] Register the new serializer with the
EventSerializerRegistry