Crate serde_with_value_affix

Crate serde_with_value_affix 

Source
Expand description

§Serde with Value Affix

Based on serde and applies a prefix / suffix to the value of a field during serialization. Note the difference vs serde_with macros: with_prefix! and with_suffix!, which apply to each field name of a struct instead.

§Example: Parsing JSON with suffix

use serde::{Deserialize, Serialize};
use serde_with_value_affix::with_affix;

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(with = "value_prefix_a")]
    code: u8,
    #[serde(with = "value_suffix_celsius")]
    temperature: f32,
}

with_affix!(value_prefix_a Prefix "A");
with_affix!(value_suffix_celsius Suffix "C");

// Serializes
MyStruct {
    code: 12,
    temperature: -12.3,
};

// into
{
  "length": "A12",
  "temperature": "-12.3C"
}

Macros§

with_affix
This macro implements the adapter to use with #[serde(with = )] that serializes with an added affix on the field value and deserialize by trimming away that affix.