diff options
Diffstat (limited to 'vendor/serde_json/tests/regression')
-rw-r--r-- | vendor/serde_json/tests/regression/issue1004.rs | 12 | ||||
-rw-r--r-- | vendor/serde_json/tests/regression/issue520.rs | 20 | ||||
-rw-r--r-- | vendor/serde_json/tests/regression/issue795.rs | 59 | ||||
-rw-r--r-- | vendor/serde_json/tests/regression/issue845.rs | 73 | ||||
-rw-r--r-- | vendor/serde_json/tests/regression/issue953.rs | 9 |
5 files changed, 173 insertions, 0 deletions
diff --git a/vendor/serde_json/tests/regression/issue1004.rs b/vendor/serde_json/tests/regression/issue1004.rs new file mode 100644 index 0000000..c09fb96 --- /dev/null +++ b/vendor/serde_json/tests/regression/issue1004.rs @@ -0,0 +1,12 @@ +#![cfg(feature = "arbitrary_precision")] + +#[test] +fn test() { + let float = 5.55f32; + let value = serde_json::to_value(float).unwrap(); + let json = serde_json::to_string(&value).unwrap(); + + // If the f32 were cast to f64 by Value before serialization, then this + // would incorrectly serialize as 5.550000190734863. + assert_eq!(json, "5.55"); +} diff --git a/vendor/serde_json/tests/regression/issue520.rs b/vendor/serde_json/tests/regression/issue520.rs new file mode 100644 index 0000000..9ed3677 --- /dev/null +++ b/vendor/serde_json/tests/regression/issue520.rs @@ -0,0 +1,20 @@ +#![allow(clippy::float_cmp)] + +use serde_derive::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug)] +#[serde(tag = "type", content = "data")] +enum E { + Float(f32), +} + +#[test] +fn test() { + let e = E::Float(159.1); + let v = serde_json::to_value(e).unwrap(); + let e = serde_json::from_value::<E>(v).unwrap(); + + match e { + E::Float(f) => assert_eq!(f, 159.1), + } +} diff --git a/vendor/serde_json/tests/regression/issue795.rs b/vendor/serde_json/tests/regression/issue795.rs new file mode 100644 index 0000000..bb82852 --- /dev/null +++ b/vendor/serde_json/tests/regression/issue795.rs @@ -0,0 +1,59 @@ +#![allow(clippy::assertions_on_result_states)] + +use serde::de::{ + Deserialize, Deserializer, EnumAccess, IgnoredAny, MapAccess, VariantAccess, Visitor, +}; +use serde_json::json; +use std::fmt; + +#[derive(Debug)] +pub enum Enum { + Variant { x: u8 }, +} + +impl<'de> Deserialize<'de> for Enum { + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> + where + D: Deserializer<'de>, + { + struct EnumVisitor; + + impl<'de> Visitor<'de> for EnumVisitor { + type Value = Enum; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("enum Enum") + } + + fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> + where + A: EnumAccess<'de>, + { + let (IgnoredAny, variant) = data.variant()?; + variant.struct_variant(&["x"], self) + } + + fn visit_map<A>(self, mut data: A) -> Result<Self::Value, A::Error> + where + A: MapAccess<'de>, + { + let mut x = 0; + if let Some((IgnoredAny, value)) = data.next_entry()? { + x = value; + } + Ok(Enum::Variant { x }) + } + } + + deserializer.deserialize_enum("Enum", &["Variant"], EnumVisitor) + } +} + +#[test] +fn test() { + let s = r#" {"Variant":{"x":0,"y":0}} "#; + assert!(serde_json::from_str::<Enum>(s).is_err()); + + let j = json!({"Variant":{"x":0,"y":0}}); + assert!(serde_json::from_value::<Enum>(j).is_err()); +} diff --git a/vendor/serde_json/tests/regression/issue845.rs b/vendor/serde_json/tests/regression/issue845.rs new file mode 100644 index 0000000..e8b0c0f --- /dev/null +++ b/vendor/serde_json/tests/regression/issue845.rs @@ -0,0 +1,73 @@ +#![allow(clippy::trait_duplication_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/8757 + +use serde::{Deserialize, Deserializer}; +use std::fmt::{self, Display}; +use std::marker::PhantomData; +use std::str::FromStr; + +pub struct NumberVisitor<T> { + marker: PhantomData<T>, +} + +impl<'de, T> serde::de::Visitor<'de> for NumberVisitor<T> +where + T: TryFrom<u64> + TryFrom<i64> + FromStr, + <T as TryFrom<u64>>::Error: Display, + <T as TryFrom<i64>>::Error: Display, + <T as FromStr>::Err: Display, +{ + type Value = T; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("an integer or string") + } + + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> + where + E: serde::de::Error, + { + T::try_from(v).map_err(serde::de::Error::custom) + } + + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> + where + E: serde::de::Error, + { + T::try_from(v).map_err(serde::de::Error::custom) + } + + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> + where + E: serde::de::Error, + { + v.parse().map_err(serde::de::Error::custom) + } +} + +fn deserialize_integer_or_string<'de, D, T>(deserializer: D) -> Result<T, D::Error> +where + D: Deserializer<'de>, + T: TryFrom<u64> + TryFrom<i64> + FromStr, + <T as TryFrom<u64>>::Error: Display, + <T as TryFrom<i64>>::Error: Display, + <T as FromStr>::Err: Display, +{ + deserializer.deserialize_any(NumberVisitor { + marker: PhantomData, + }) +} + +#[derive(Deserialize, Debug)] +pub struct Struct { + #[serde(deserialize_with = "deserialize_integer_or_string")] + pub i: i64, +} + +#[test] +fn test() { + let j = r#" {"i":100} "#; + println!("{:?}", serde_json::from_str::<Struct>(j).unwrap()); + + let j = r#" {"i":"100"} "#; + println!("{:?}", serde_json::from_str::<Struct>(j).unwrap()); +} diff --git a/vendor/serde_json/tests/regression/issue953.rs b/vendor/serde_json/tests/regression/issue953.rs new file mode 100644 index 0000000..771aa52 --- /dev/null +++ b/vendor/serde_json/tests/regression/issue953.rs @@ -0,0 +1,9 @@ +use serde_json::Value; + +#[test] +fn test() { + let x1 = serde_json::from_str::<Value>("18446744073709551615."); + assert!(x1.is_err()); + let x2 = serde_json::from_str::<Value>("18446744073709551616."); + assert!(x2.is_err()); +} |