forked from platypush/platypush
Message
objects enhancements.
- Support smart JSON serialization of `Message` objects. - Added `Message.to_dict` method.
This commit is contained in:
parent
5ad1a62293
commit
a6d6fd4067
1 changed files with 17 additions and 0 deletions
|
@ -84,6 +84,9 @@ class Message:
|
||||||
if is_dataclass(obj):
|
if is_dataclass(obj):
|
||||||
return asdict(obj)
|
return asdict(obj)
|
||||||
|
|
||||||
|
if isinstance(obj, Message):
|
||||||
|
return obj.to_dict(obj)
|
||||||
|
|
||||||
# Don't serialize I/O wrappers/objects
|
# Don't serialize I/O wrappers/objects
|
||||||
if isinstance(obj, io.IOBase):
|
if isinstance(obj, io.IOBase):
|
||||||
return None
|
return None
|
||||||
|
@ -168,6 +171,20 @@ class Message:
|
||||||
|
|
||||||
return msg
|
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
|
@classmethod
|
||||||
def build(cls, msg):
|
def build(cls, msg):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue