From 720d841b176a499fd8622557508b1d20011ca74a Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Sat, 11 Apr 2020 02:53:29 +0200 Subject: [PATCH] Show simple way we could (de)serialize the whole amino type/value spiel --- tendermint/src/amino_types.rs | 1 + tendermint/src/amino_types/registered_json.rs | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tendermint/src/amino_types/registered_json.rs diff --git a/tendermint/src/amino_types.rs b/tendermint/src/amino_types.rs index 778e99c77..ea647fcfa 100644 --- a/tendermint/src/amino_types.rs +++ b/tendermint/src/amino_types.rs @@ -8,6 +8,7 @@ pub mod ed25519; pub mod message; pub mod ping; pub mod proposal; +mod registered_json; pub mod remote_error; pub mod signature; pub mod time; diff --git a/tendermint/src/amino_types/registered_json.rs b/tendermint/src/amino_types/registered_json.rs new file mode 100644 index 000000000..531c369d4 --- /dev/null +++ b/tendermint/src/amino_types/registered_json.rs @@ -0,0 +1,36 @@ +#[cfg(test)] +mod test { + use crate::test::test_serialization_roundtrip; + use serde::de::DeserializeOwned; + use serde::{Deserialize, Serialize}; + + #[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] + struct MsgUnjail { + #[serde(alias = "address")] + validator_addr: String, + // NOTE: Above shouldn't be a string but: validator_addr: Vec, + // In reality you would need to tell serde how to read bechifyed addresses instead! + // but this is orthogonal to what this code wants to show. + } + + // TODO: deserves a better name + #[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] + struct AminoJSON { + #[serde(alias = "type")] + pub type_name: Option, + #[serde(bound(deserialize = "Option: Deserialize<'de>"))] + pub value: Option, + } + + #[test] + fn test_simple_example() { + let json_data = + r#"{"type":"cosmos-sdk/MsgUnjail","value":{"address":"cosmosvaloper1v93xxeqhg9nn6"}}"#; + let res = serde_json::from_str::>(json_data); + println!("res: {:?}", res); + assert!(res.is_ok()); + let msg_unjail = res.unwrap().value; + println!("{:?}", msg_unjail); + test_serialization_roundtrip::>(&json_data); + } +}