Unverified Commit 840221dd authored by Thomas Cameron's avatar Thomas Cameron Committed by GitHub
Browse files

Add serde support to number type (#2645)

## Motivation and Context
This is a child PR of https://github.com/awslabs/smithy-rs/pull/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: default avatarJohn DiSanti <johndisanti@gmail.com>
Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>
parent b30b063e
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
@@ -3,11 +3,33 @@
 * SPDX-License-Identifier: Apache-2.0
 */

//! A number type that implements Javascript / JSON semantics.

use crate::error::{TryFromNumberError, TryFromNumberErrorKind};
#[cfg(all(
    aws_sdk_unstable,
    any(feature = "serde-serialize", feature = "serde-deserialize")
))]
use serde;

/// A number type that implements Javascript / JSON semantics, modeled on serde_json:
/// <https://docs.serde.rs/src/serde_json/number.rs.html#20-22>
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(
    all(aws_sdk_unstable, feature = "serde-deserialize"),
    derive(serde::Deserialize)
)]
#[cfg_attr(
    all(aws_sdk_unstable, feature = "serde-serialize"),
    derive(serde::Serialize)
)]
#[cfg_attr(
    any(
        all(aws_sdk_unstable, feature = "serde-deserialize"),
        all(aws_sdk_unstable, feature = "serde-serialize")
    ),
    serde(untagged)
)]
pub enum Number {
    /// Unsigned 64-bit integer value.
    PosInt(u64),
@@ -441,4 +463,31 @@ mod test {
            1452089100f32
        );
    }

    #[test]
    #[cfg(all(
        test,
        aws_sdk_unstable,
        feature = "serde-deserialize",
        feature = "serde-serialize"
    ))]
    /// ensures that numbers are deserialized as expected
    /// 0 <= PosInt
    /// 0 > NegInt
    /// non integer values == Float
    fn number_serde() {
        let n: Number = serde_json::from_str("1.1").unwrap();
        assert_eq!(n, Number::Float(1.1));
        let n: Number = serde_json::from_str("1").unwrap();
        assert_eq!(n, Number::PosInt(1));
        let n: Number = serde_json::from_str("0").unwrap();
        assert_eq!(n, Number::PosInt(0));
        let n: Number = serde_json::from_str("-1").unwrap();
        assert_eq!(n, Number::NegInt(-1));

        assert_eq!("1.1", serde_json::to_string(&Number::Float(1.1)).unwrap());
        assert_eq!("1", serde_json::to_string(&Number::PosInt(1)).unwrap());
        assert_eq!("0", serde_json::to_string(&Number::PosInt(0)).unwrap());
        assert_eq!("-1", serde_json::to_string(&Number::NegInt(-1)).unwrap());
    }
}