pub trait PayloadSerialize: Clone {
type Error: Debug + Into<Box<dyn Error + Sync + Send + 'static>>;
// Required methods
fn serialize(self) -> Result<SerializedPayload, Self::Error>;
fn deserialize(
payload: &[u8],
content_type: Option<&String>,
format_indicator: &FormatIndicator,
) -> Result<Self, DeserializationError<Self::Error>>;
}
Expand description
Trait for serializing and deserializing payloads.
§Examples
use azure_iot_operations_protocol::common::payload_serialize::{PayloadSerialize, DeserializationError, FormatIndicator, SerializedPayload};
#[derive(Clone, Debug)]
pub struct CarLocationResponse {
latitude: f64,
longitude: f64,
}
impl PayloadSerialize for CarLocationResponse {
type Error = String;
fn serialize(self) -> Result<SerializedPayload, String> {
let response = format!("{{\"latitude\": {}, \"longitude\": {}}}", self.latitude, self.longitude);
Ok(SerializedPayload {
payload: response.as_bytes().to_vec(),
content_type: "application/json".to_string(),
format_indicator: FormatIndicator::Utf8EncodedCharacterData,
})
}
fn deserialize(payload: &[u8],
content_type: Option<&String>,
_format_indicator: &FormatIndicator,
) -> Result<Self, DeserializationError<String>> {
if let Some(content_type) = content_type {
if content_type != "application/json" {
return Err(DeserializationError::UnsupportedContentType(format!(
"Invalid content type: '{content_type:?}'. Must be 'application/json'"
)));
}
}
// mock deserialization here for brevity
let _payload = String::from_utf8(payload.to_vec()).unwrap();
Ok(CarLocationResponse {latitude: 12.0, longitude: 35.0})
}
}
Required Associated Types§
Required Methods§
Sourcefn serialize(self) -> Result<SerializedPayload, Self::Error>
fn serialize(self) -> Result<SerializedPayload, Self::Error>
Serializes the payload from the generic type to a byte vector and specifies the content type and format indicator. The content type and format indicator could be the same every time or dynamic per payload.
§Errors
Returns a PayloadSerialize::Error
if the serialization fails.
Sourcefn deserialize(
payload: &[u8],
content_type: Option<&String>,
format_indicator: &FormatIndicator,
) -> Result<Self, DeserializationError<Self::Error>>
fn deserialize( payload: &[u8], content_type: Option<&String>, format_indicator: &FormatIndicator, ) -> Result<Self, DeserializationError<Self::Error>>
Deserializes the payload from a byte vector to the generic type
§Errors
Returns a DeserializationError::InvalidPayload
over type PayloadSerialize::Error
if the deserialization fails.
Returns a DeserializationError::UnsupportedContentType
if the content type isn’t supported by this deserialization implementation.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl PayloadSerialize for Vec<u8>
Provided convenience implementation for sending raw bytes as content_type
“application/octet-stream”.
impl PayloadSerialize for Vec<u8>
Provided convenience implementation for sending raw bytes as content_type
“application/octet-stream”.