Skip to content

compas_eve.codecs ¤

Classes¤

JsonMessageCodec ¤

JSON codec for message serialization.

This codec uses the COMPAS framework's JSON serialization functions to encode and decode message data. It can handle Message objects, COMPAS Data objects, and regular dictionaries.

Functions¤

decode ¤
decode(encoded_data: bytes, message_type: type) -> Message

Decode JSON message payloads to message object.

Parameters:

  • encoded_data (bytes) –

    Message bytes to decode into a JSON string.

  • message_type (type) –

    The message type class to use for parsing.

Returns:

  • Message

    Decoded message object.

encode ¤
encode(message: Message | dict | Any) -> str

Encode a message to JSON string.

Parameters:

  • message (Message | dict | Any) –

    Message to encode. Can be a Message instance, a dict, or an object implementing the COMPAS data framework.

Returns:

  • str

    JSON string representation of the message.

MessageCodec ¤

Abstract base class for message codecs.

A codec is responsible for encoding and decoding messages to/from a specific representation format (e.g., JSON, Protocol Buffers).

Functions¤

decode ¤
decode(encoded_data: bytes) -> Message | dict | Any

Decode data from the codec's representation format.

Parameters:

  • encoded_data (bytes) –

    Encoded data to decode.

Returns:

  • Message | dict | Any

    Decoded message after reconstruction from the encoded data.

encode ¤
encode(message: Message | dict | Any) -> bytes | str

Encode a message to the codec's representation format.

Parameters:

  • message (Message | dict | Any) –

    Message to encode. Can be a Message instance, a dict, or an object implementing the COMPAS data framework.

Returns:

  • bytes or str

    Encoded representation of the message.

ProtobufMessageCodec ¤

ProtobufMessageCodec()

Protocol Buffers codec for message serialization.

This codec uses the compas_pb package to encode and decode message data using Protocol Buffers binary format.

Note

This codec requires the compas_pb package to be installed. If compas_pb is not available, attempting to encode or decode will raise an ImportError.

Functions¤

decode ¤
decode(encoded_data: bytes, message_type: type | None = None) -> object

Decode Protocol Buffers binary data to message object.

Parameters:

  • encoded_data (bytes) –

    Protocol Buffers binary data to decode.

  • message_type (type | None, default: None ) –

    The message type class (not used for protobuf as it's encoded in the data).

Returns:

  • object

    Decoded message object.

encode ¤
encode(message: Message | dict | Any) -> bytes

Encode a message to Protocol Buffers binary format.

Parameters:

  • message (Message | dict | Any) –

    Message to encode. Can be a Message instance, a dict, or an object implementing the COMPAS data framework.

Returns:

  • bytes

    Protocol Buffers binary representation of the message.