From 3a53f5165b8121f193b9fdbe67a8f9db7daba1f3 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 29 Nov 2022 11:48:10 +0100 Subject: [PATCH 1/3] Add #[serde(flatten)] tests for serialization and deserialization --- src/de/mod.rs | 35 ++++++++++++++++++++++++++++++++++- src/ser/mod.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/de/mod.rs b/src/de/mod.rs index edb56131..1c6fd50d 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -687,7 +687,7 @@ where #[cfg(test)] mod tests { use super::from_str; - use serde_derive::Deserialize; + use serde_derive::{Deserialize, Serialize}; #[derive(Debug, Deserialize, PartialEq)] enum Type { @@ -1018,6 +1018,39 @@ mod tests { assert_eq!(serde_json::from_str::(r#"null"#).unwrap(), Nothing); } + #[test] + fn struct_with_flatten() { + #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] + struct Pagination { + limit: u64, + offset: u64, + total: u64, + } + + #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] + struct Users { + users: Vec, + + #[serde(flatten)] + pagination: Pagination, + } + + let expected = Users { + users: vec!["joe".to_string(), "alice".to_string()], + pagination: Pagination { + offset: 100, + limit: 20, + total: 102, + }, + }; + + assert_eq!( + from_str::(r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"#) + .unwrap(), + expected, + ); + } + #[test] fn ignoring_extra_fields() { #[derive(Debug, Deserialize, PartialEq)] diff --git a/src/ser/mod.rs b/src/ser/mod.rs index db009914..fbe1d746 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -539,7 +539,7 @@ impl ser::SerializeStructVariant for Unreachable { mod tests { use super::to_string; - use serde_derive::Serialize; + use serde_derive::{Deserialize, Serialize}; #[test] fn bool() { @@ -988,6 +988,38 @@ mod tests { ); } + #[test] + fn struct_with_flatten() { + #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] + struct Pagination { + limit: u64, + offset: u64, + total: u64, + } + + #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] + struct Users { + users: Vec, + + #[serde(flatten)] + pagination: Pagination, + } + + let users = Users { + users: vec!["joe".to_string(), "alice".to_string()], + pagination: Pagination { + offset: 100, + limit: 20, + total: 102, + }, + }; + + assert_eq!( + to_string(&users).unwrap(), + r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"# + ); + } + #[test] fn btree_map() { use std::collections::BTreeMap; From 5ac78a2b539fb117cbb768a8512e57e606d35776 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 29 Nov 2022 11:49:32 +0100 Subject: [PATCH 2/3] Test serde_json compliance --- src/ser/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ser/mod.rs b/src/ser/mod.rs index fbe1d746..f6160638 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -1018,6 +1018,11 @@ mod tests { to_string(&users).unwrap(), r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"# ); + assert_eq!( + to_string(&users).unwrap(), + serde_json::to_string(&users).unwrap(), + "serialization must match serde_json implementation" + ); } #[test] From 56c81ec1aa0b7e18a2046147896c4dfb1044926c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 29 Nov 2022 11:54:47 +0100 Subject: [PATCH 3/3] Add CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index da435c86..3c54589d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ project adheres to [Semantic Versioning](http://semver.org/). ### Added - Add support for map (de)serialization. +- Add support for `#[serde(flatten)]` (de)serialization ([#20]). + +[#20]: https://github.com/CosmWasm/serde-json-wasm/issues/20 ### Changed