Macro serde_with::with_prefix[][src]

macro_rules! with_prefix {
    ($module:ident $prefix:expr) => { ... };
}
Expand description

Serialize with an added prefix on every field name and deserialize by trimming away the prefix.

Note: Use of this macro is incompatible with applying the deny_unknown_fields attribute on the container. While deserializing, it will always warn about unknown fields, even though they are processed by the with_prefix wrapper. More details can be found in this issue.

Example

The Challonge REST API likes to use prefixes to group related fields. In simplified form, their JSON may resemble the following:

{
  "player1_name": "name1",
  "player1_votes": 1,
  "player2_name": "name2",
  "player2_votes": 2
}

In Rust we would ideally like to model this data as a pair of Player structs, rather than repeating the fields of Player for each prefix.

struct Match {
    player1: Player,
    player2: Player,
}

struct Player {
    name: String,
    votes: u64,
}

This with_prefix! macro produces an adapter that adds a prefix onto field names during serialization and trims away the prefix during deserialization. An implementation of the Challonge API would use with_prefix! like this:

use serde_derive::{Deserialize, Serialize};
use serde_with::with_prefix;

#[derive(Serialize, Deserialize)]
struct Match {
    #[serde(flatten, with = "prefix_player1")]
    player1: Player,
    #[serde(flatten, with = "prefix_player2")]
    player2: Player,
}

#[derive(Serialize, Deserialize)]
struct Player {
    name: String,
    votes: u64,
}

with_prefix!(prefix_player1 "player1_");
with_prefix!(prefix_player2 "player2_");

fn main() {
    let m = Match {
        player1: Player {
            name: "name1".to_owned(),
            votes: 1,
        },
        player2: Player {
            name: "name2".to_owned(),
            votes: 2,
        },
    };

    let j = serde_json::to_string_pretty(&m).unwrap();
    println!("{}", j);
}