Struct axum_codec::Codec
source · pub struct Codec<T>(pub T);Expand description
Codec extractor / response.
The serialized data is not specified, unlike [axum::Json]. Upon deserialization, the request’s
Content-Type header is used to determine the format of the data.
The supported formats are:
JSONMessagePackBincodeBitcodeYAMLTOML
By default, only JSON is supported. To enable other formats, use the corresponding feature flags.
Note that IntoResponse is not implemented for this type, as the headers are not available
when serializing the data. Instead, use Codec::to_response to create a response with the
appropriate Content-Type header extracted from the request with Accept.
§Examples
#[axum_codec::apply(decode)]
struct Greeting {
hello: String
}
let bytes = b"{\"hello\": \"world\"}";
let content_type = ContentType::Json;
let Codec(data) = Codec::<Greeting>::from_bytes(bytes, content_type).unwrap();
assert_eq!(data.hello, "world");Tuple Fields§
§0: TImplementations§
source§impl<T> Codec<T>where
T: DeserializeOwned,
impl<T> Codec<T>where
T: DeserializeOwned,
source§impl<T> Codec<T>
impl<T> Codec<T>
sourcepub fn from_bytes(
bytes: &[u8],
content_type: ContentType,
) -> Result<Self, CodecRejection>where
T: CodecDecode,
pub fn from_bytes(
bytes: &[u8],
content_type: ContentType,
) -> Result<Self, CodecRejection>where
T: CodecDecode,
source§impl<T> Codec<T>
impl<T> Codec<T>
sourcepub fn to_bytes(&self, content_type: ContentType) -> Result<Vec<u8>, Error>where
T: CodecEncode,
pub fn to_bytes(&self, content_type: ContentType) -> Result<Vec<u8>, Error>where
T: CodecEncode,
source§impl<T> Codec<T>where
T: CodecEncode,
impl<T> Codec<T>where
T: CodecEncode,
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the Codec and returns the inner value.
sourcepub fn to_response<C: Into<ContentType>>(&self, content_type: C) -> Response
pub fn to_response<C: Into<ContentType>>(&self, content_type: C) -> Response
Converts the inner value into a response with the given content type.
If serialization fails, the rejection is converted into a response. See [encode::Error] for possible errors.