Skip to content

Commit 840221d

Browse files
Add serde support to number type (#2645)
## Motivation and Context This is a child PR of #2616 The changes that this PR introduces is same as the ones that were merged to `unstable-serde-support` branch before. Initially, we tried to make commit to unstable-serde-support branch and merge changes one by one in small PRs. However, in order to make it up to date with the main branch, we would need to go through a large PR of over 700 files. Thus, I decided to create individual PRs that commits directly to `main` branch. ## Description - Implements `serde` support to `Number` ## Testing - Test checks whether the serialized/de-serialized data matches with the expected value ## Checklist NA ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --------- Co-authored-by: John DiSanti <johndisanti@gmail.com> Co-authored-by: John DiSanti <jdisanti@amazon.com>
1 parent b30b063 commit 840221d

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

rust-runtime/aws-smithy-types/src/number.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,33 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
//! A number type that implements Javascript / JSON semantics.
7+
68
use crate::error::{TryFromNumberError, TryFromNumberErrorKind};
9+
#[cfg(all(
10+
aws_sdk_unstable,
11+
any(feature = "serde-serialize", feature = "serde-deserialize")
12+
))]
13+
use serde;
714

815
/// A number type that implements Javascript / JSON semantics, modeled on serde_json:
916
/// <https://docs.serde.rs/src/serde_json/number.rs.html#20-22>
1017
#[derive(Debug, Clone, Copy, PartialEq)]
18+
#[cfg_attr(
19+
all(aws_sdk_unstable, feature = "serde-deserialize"),
20+
derive(serde::Deserialize)
21+
)]
22+
#[cfg_attr(
23+
all(aws_sdk_unstable, feature = "serde-serialize"),
24+
derive(serde::Serialize)
25+
)]
26+
#[cfg_attr(
27+
any(
28+
all(aws_sdk_unstable, feature = "serde-deserialize"),
29+
all(aws_sdk_unstable, feature = "serde-serialize")
30+
),
31+
serde(untagged)
32+
)]
1133
pub enum Number {
1234
/// Unsigned 64-bit integer value.
1335
PosInt(u64),
@@ -441,4 +463,31 @@ mod test {
441463
1452089100f32
442464
);
443465
}
466+
467+
#[test]
468+
#[cfg(all(
469+
test,
470+
aws_sdk_unstable,
471+
feature = "serde-deserialize",
472+
feature = "serde-serialize"
473+
))]
474+
/// ensures that numbers are deserialized as expected
475+
/// 0 <= PosInt
476+
/// 0 > NegInt
477+
/// non integer values == Float
478+
fn number_serde() {
479+
let n: Number = serde_json::from_str("1.1").unwrap();
480+
assert_eq!(n, Number::Float(1.1));
481+
let n: Number = serde_json::from_str("1").unwrap();
482+
assert_eq!(n, Number::PosInt(1));
483+
let n: Number = serde_json::from_str("0").unwrap();
484+
assert_eq!(n, Number::PosInt(0));
485+
let n: Number = serde_json::from_str("-1").unwrap();
486+
assert_eq!(n, Number::NegInt(-1));
487+
488+
assert_eq!("1.1", serde_json::to_string(&Number::Float(1.1)).unwrap());
489+
assert_eq!("1", serde_json::to_string(&Number::PosInt(1)).unwrap());
490+
assert_eq!("0", serde_json::to_string(&Number::PosInt(0)).unwrap());
491+
assert_eq!("-1", serde_json::to_string(&Number::NegInt(-1)).unwrap());
492+
}
444493
}

0 commit comments

Comments
 (0)