diff options
Diffstat (limited to 'third_party/rust/serde_json/tests/regression')
4 files changed, 162 insertions, 0 deletions
diff --git a/third_party/rust/serde_json/tests/regression/issue520.rs b/third_party/rust/serde_json/tests/regression/issue520.rs new file mode 100644 index 0000000000..9ed367731b --- /dev/null +++ b/third_party/rust/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/third_party/rust/serde_json/tests/regression/issue795.rs b/third_party/rust/serde_json/tests/regression/issue795.rs new file mode 100644 index 0000000000..bb82852c54 --- /dev/null +++ b/third_party/rust/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/third_party/rust/serde_json/tests/regression/issue845.rs b/third_party/rust/serde_json/tests/regression/issue845.rs new file mode 100644 index 0000000000..56037ae669 --- /dev/null +++ b/third_party/rust/serde_json/tests/regression/issue845.rs @@ -0,0 +1,74 @@ +#![allow(clippy::trait_duplication_in_bounds)] // https://github.com/rust-lang/rust-clippy/issues/8757 + +use serde::{Deserialize, Deserializer}; +use std::convert::TryFrom; +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/third_party/rust/serde_json/tests/regression/issue953.rs b/third_party/rust/serde_json/tests/regression/issue953.rs new file mode 100644 index 0000000000..771aa5287c --- /dev/null +++ b/third_party/rust/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()); +} |