summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_with/tests/with_prefix.rs
blob: b1113b7ceebcf5e2c31f1b35827bf871d8ae03be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
extern crate pretty_assertions;
extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate serde_with;

use pretty_assertions::assert_eq;
use serde_derive::{Deserialize, Serialize};
use serde_json::json;
use serde_with::with_prefix;
use std::collections::HashMap;

#[test]
fn test_flatten_with_prefix() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Match {
        #[serde(flatten, with = "prefix_player1")]
        player1: Player,
        #[serde(flatten, with = "prefix_player2")]
        player2: Option<Player>,
        #[serde(flatten, with = "prefix_player3")]
        player3: Option<Player>,
        #[serde(flatten, with = "prefix_tag")]
        tags: HashMap<String, String>,
    }

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

    with_prefix!(prefix_player1 "player1_");
    with_prefix!(prefix_player2 "player2_");
    with_prefix!(prefix_player3 "player3_");
    with_prefix!(prefix_tag "tag_");

    let m = Match {
        player1: Player {
            name: "name1".to_owned(),
            votes: 1,
        },
        player2: Some(Player {
            name: "name2".to_owned(),
            votes: 2,
        }),
        player3: None,
        tags: {
            let mut tags = HashMap::new();
            tags.insert("t".to_owned(), "T".to_owned());
            tags
        },
    };

    let expected = json!({
        "player1_name": "name1",
        "player1_votes": 1,
        "player2_name": "name2",
        "player2_votes": 2,
        "tag_t": "T"
    });

    let j = serde_json::to_string(&m).unwrap();
    assert_eq!(j, expected.to_string());

    assert_eq!(m, serde_json::from_str(&j).unwrap());
}

#[test]
fn test_plain_with_prefix() {
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Match {
        #[serde(with = "prefix_player1")]
        player1: Player,
        #[serde(with = "prefix_player2")]
        player2: Option<Player>,
        #[serde(with = "prefix_player3")]
        player3: Option<Player>,
        #[serde(with = "prefix_tag")]
        tags: HashMap<String, String>,
    }

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

    with_prefix!(prefix_player1 "player1_");
    with_prefix!(prefix_player2 "player2_");
    with_prefix!(prefix_player3 "player3_");
    with_prefix!(prefix_tag "tag_");

    let m = Match {
        player1: Player {
            name: "name1".to_owned(),
            votes: 1,
        },
        player2: Some(Player {
            name: "name2".to_owned(),
            votes: 2,
        }),
        player3: None,
        tags: {
            let mut tags = HashMap::new();
            tags.insert("t".to_owned(), "T".to_owned());
            tags
        },
    };

    let expected = json!({
        "player1": {
            "player1_name": "name1",
            "player1_votes": 1,
        },
        "player2": {
            "player2_name": "name2",
            "player2_votes": 2,
        },
        "player3": null,
        "tags": {
            "tag_t": "T"
        }
    });

    let j = serde_json::to_string(&m).unwrap();
    assert_eq!(j, expected.to_string());

    assert_eq!(m, serde_json::from_str(&j).unwrap());
}