1313 :toctree: generated/
1414 :nosignatures:
1515
16+ MessageCodec
17+ JsonMessageCodec
1618 ProtobufMessageCodec
1719
1820"""
1921
20- from compas_eve .core import MessageCodec
22+ from compas .data import json_dumps
23+ from compas .data import json_loads
2124
22- __all__ = ["ProtobufMessageCodec" ]
25+ __all__ = ["MessageCodec" , "JsonMessageCodec" , "ProtobufMessageCodec" ]
26+
27+
28+ class MessageCodec (object ):
29+ """Abstract base class for message codecs.
30+
31+ A codec is responsible for encoding and decoding messages
32+ to/from a specific representation format (e.g., JSON, Protocol Buffers).
33+ """
34+
35+ def encode (self , message ):
36+ """Encode a message to the codec's representation format.
37+
38+ Parameters
39+ ----------
40+ message : :class:`Message` or dict or object
41+ Message to encode. Can be a Message instance, a dict, or
42+ an object implementing the COMPAS data framework.
43+
44+ Returns
45+ -------
46+ bytes or str
47+ Encoded representation of the message.
48+ """
49+ raise NotImplementedError ("Subclasses must implement encode()" )
50+
51+ def decode (self , encoded_data ):
52+ """Decode data from the codec's representation format.
53+
54+ Parameters
55+ ----------
56+ encoded_data : bytes or str
57+ Encoded data to decode.
58+
59+ Returns
60+ -------
61+ dict
62+ Decoded data dictionary that can be used to reconstruct a message.
63+ """
64+ raise NotImplementedError ("Subclasses must implement decode()" )
65+
66+
67+ class JsonMessageCodec (MessageCodec ):
68+ """JSON codec for message serialization.
69+
70+ This codec uses the COMPAS framework's JSON serialization functions
71+ to encode and decode message data. It can handle Message objects,
72+ COMPAS Data objects, and regular dictionaries.
73+ """
74+
75+ def encode (self , message ):
76+ """Encode a message to JSON string.
77+
78+ Parameters
79+ ----------
80+ message : :class:`Message` or dict or object
81+ Message to encode. Can be a Message instance, a dict, or
82+ an object implementing the COMPAS data framework.
83+
84+ Returns
85+ -------
86+ str
87+ JSON string representation of the message.
88+ """
89+ # Extract data from the message
90+ try :
91+ data = message .data
92+ except (KeyError , AttributeError ):
93+ try :
94+ data = message .__data__
95+ except (KeyError , AttributeError ):
96+ data = dict (message )
97+ return json_dumps (data )
98+
99+ def decode (self , encoded_data , message_type ):
100+ """Decode JSON string to message object.
101+
102+ Parameters
103+ ----------
104+ encoded_data : str
105+ JSON string to decode.
106+ message_type : type
107+ The message type class to use for parsing.
108+
109+ Returns
110+ -------
111+ :class:`Message`
112+ Decoded message object.
113+ """
114+ data = json_loads (encoded_data )
115+ return message_type .parse (data )
23116
24117
25118try :
@@ -34,8 +127,7 @@ class ProtobufMessageCodec(MessageCodec):
34127 """Protocol Buffers codec for message serialization.
35128
36129 This codec uses the compas_pb package to encode and decode message data
37- using Protocol Buffers binary format. It can handle Message objects,
38- COMPAS Data objects, and regular dictionaries.
130+ using Protocol Buffers binary format.
39131
40132 Note
41133 ----
@@ -71,32 +163,26 @@ def encode(self, message):
71163 "The ProtobufMessageCodec requires 'compas_pb' to be installed. "
72164 "Please install it with: pip install compas_pb"
73165 )
74- # Extract data from the message
75- try :
76- data = message .data
77- except (KeyError , AttributeError ):
78- try :
79- data = message .__data__
80- except (KeyError , AttributeError ):
81- data = dict (message )
82- return compas_pb .encode (data )
166+ return compas_pb .pb_dump_bts (message )
83167
84- def decode (self , encoded_data ):
85- """Decode Protocol Buffers binary data to data dictionary .
168+ def decode (self , encoded_data , message_type = None ):
169+ """Decode Protocol Buffers binary data to message object .
86170
87171 Parameters
88172 ----------
89173 encoded_data : bytes
90174 Protocol Buffers binary data to decode.
175+ message_type : type, optional
176+ The message type class (not used for protobuf as it's encoded in the data).
91177
92178 Returns
93179 -------
94- dict
95- Decoded data dictionary .
180+ object
181+ Decoded message object .
96182 """
97183 if not COMPAS_PB_AVAILABLE :
98184 raise ImportError (
99185 "The ProtobufMessageCodec requires 'compas_pb' to be installed. "
100186 "Please install it with: pip install compas_pb"
101187 )
102- return compas_pb .decode (encoded_data )
188+ return compas_pb .pb_load_bts (encoded_data )
0 commit comments