`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
1 changed files with 17 additions and 0 deletions

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):
"""