Skip to main content

ResponseSerializer

Trait ResponseSerializer 

Source
pub trait ResponseSerializer<T>:
    Send
    + Sync
    + 'static {
    type Output: Serialize;

    // Required method
    fn serialize(value: T) -> Self::Output;
}
Expand description

ResponseSerializer Trait

A trait for defining custom response serialization logic. Use this when you want to transform a domain object into a specific API response format, such as hiding fields, renaming keys, or flattening structures.

§Type Parameters

  • T: The input type to serialize

§Associated Types

  • Output: The serialized output type (must implement Serialize)

§Example

struct UserResponseSerializer;
impl ResponseSerializer<User> for UserResponseSerializer {
    type Output = UserResponse;
    fn serialize(value: User) -> Self::Output {
        UserResponse {
            id: value.id,
            name: value.name,
            // password is hidden
        }
    }
}

Required Associated Types§

Required Methods§

Source

fn serialize(value: T) -> Self::Output

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.

Implementors§