mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-29 02:37:55 +00:00
Add serialize, deserialize specific rename
Adds the rename_serialize and rename_deserialize field attributes to specify serialisation and deserialisation specific renames.
This commit is contained in:
@@ -20,6 +20,13 @@ struct Rename {
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
struct DirectionRename {
|
||||
a1: i32,
|
||||
#[serde(rename_serialize="a3", rename_deserialize="a4")]
|
||||
a2: i32,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default() {
|
||||
let deserialized_value: Default = json::from_str(&"{\"a1\":1,\"a2\":2}").unwrap();
|
||||
@@ -38,3 +45,13 @@ fn test_rename() {
|
||||
let deserialized_value: Rename = json::from_str(&serialized_value).unwrap();
|
||||
assert_eq!(value, deserialized_value);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_direction_rename() {
|
||||
let value = DirectionRename { a1: 1, a2: 2 };
|
||||
let serialized_value = json::to_string(&value).unwrap();
|
||||
assert_eq!(serialized_value, "{\"a1\":1,\"a3\":2}");
|
||||
|
||||
let deserialized_value = json::from_str("{\"a1\":1,\"a4\":2}").unwrap();
|
||||
assert_eq!(value, deserialized_value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user