Skip to main content

serialize_with

Function serialize_with 

pub fn serialize_with<T, S, F>(
    value: &T,
    serializer: S,
    transform: F,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where T: Serialize, S: Serializer, F: Fn(&str) -> String,
Expand description

Serialize a value as a singleton map with custom key transformation.

This function is similar to singleton_map::serialize but allows you to transform the key (variant name) before serialization.

§Arguments

  • value - The value to serialize
  • serializer - The serializer to use
  • transform - A function that transforms the variant name

§Examples

use serde::{Serialize, Serializer};

fn my_serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
    T: Serialize,
    S: Serializer,
{
    noyalib::with::singleton_map_with::serialize_with(
        value,
        serializer,
        |s| s.to_lowercase(),
    )
}