From a6d6fd40675aa1f6a375bc58a19ae08e8644c824 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 9 Dec 2023 01:20:20 +0100 Subject: [PATCH] `Message` objects enhancements. - Support smart JSON serialization of `Message` objects. - Added `Message.to_dict` method. --- platypush/message/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/platypush/message/__init__.py b/platypush/message/__init__.py index 03abb3768..67f9357cd 100644 --- a/platypush/message/__init__.py +++ b/platypush/message/__init__.py @@ -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): """