Message objects enhancements.

- Support smart JSON serialization of `Message` objects.
- Added `Message.to_dict` method.
This commit is contained in:
Fabio Manganiello 2023-12-09 01:20:20 +01:00
parent 5ad1a62293
commit a6d6fd4067
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -84,6 +84,9 @@ class Message:
if is_dataclass(obj):
return asdict(obj)
if isinstance(obj, Message):
return obj.to_dict(obj)
# Don't serialize I/O wrappers/objects
if isinstance(obj, io.IOBase):
return None
@ -168,6 +171,20 @@ class Message:
return msg
@classmethod
def to_dict(cls, msg):
"""
Converts a Message object into a dictionary
:param msg: Message object
"""
return {
k: v
for k, v in cls.parse(msg).items()
if k not in ('id', 'token', 'target', 'origin', '_timestamp')
}
@classmethod
def build(cls, msg):
"""