Class MessageCodec
java.lang.Object
dev.ua.theroer.magicutils.messaging.MessageCodec
Serializes
MessageEnvelope instances and user payloads to/from bytes.
The envelope header is encoded as compact JSON; the user payload is encoded
separately by encodePayload(Object) and carried as opaque bytes inside
the envelope. This keeps the transport free of any knowledge of user types and
lets a single codec instance be shared across transports.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a codec with a defaultObjectMapper.MessageCodec(com.fasterxml.jackson.databind.ObjectMapper mapper) Creates a codec backed by the given mapper. -
Method Summary
Modifier and TypeMethodDescriptiondecode(byte[] bytes) Deserializes envelope bytes back into anMessageEnvelope.<T> TdecodePayload(byte[] payload, Class<T> type) Deserializes payload bytes into the requested type.byte[]encode(MessageEnvelope envelope) Serializes a full envelope (header + embedded payload) to bytes.byte[]encodePayload(Object payload) Serializes a user payload object to bytes.com.fasterxml.jackson.databind.ObjectMappermapper()Returns the underlying mapper (for advanced payload handling).
-
Constructor Details
-
MessageCodec
public MessageCodec()Creates a codec with a defaultObjectMapper. -
MessageCodec
public MessageCodec(com.fasterxml.jackson.databind.ObjectMapper mapper) Creates a codec backed by the given mapper.- Parameters:
mapper- Jackson mapper used for both envelope and payloads
-
-
Method Details
-
mapper
public com.fasterxml.jackson.databind.ObjectMapper mapper()Returns the underlying mapper (for advanced payload handling).- Returns:
- object mapper
-
encodePayload
Serializes a user payload object to bytes.- Parameters:
payload- payload object (may be null → empty bytes)- Returns:
- serialized payload bytes
-
decodePayload
Deserializes payload bytes into the requested type.- Type Parameters:
T- payload type- Parameters:
payload- serialized payload bytestype- target type- Returns:
- deserialized payload, or null when the bytes are empty
-
encode
Serializes a full envelope (header + embedded payload) to bytes.- Parameters:
envelope- envelope to encode- Returns:
- serialized envelope bytes
-
decode
Deserializes envelope bytes back into anMessageEnvelope.- Parameters:
bytes- serialized envelope bytes- Returns:
- decoded envelope
-