summaryrefslogtreecommitdiffstats
path: root/vendor/serde_json/tests
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/serde_json/tests')
-rw-r--r--vendor/serde_json/tests/regression/issue845.rs1
-rw-r--r--vendor/serde_json/tests/test.rs175
2 files changed, 134 insertions, 42 deletions
diff --git a/vendor/serde_json/tests/regression/issue845.rs b/vendor/serde_json/tests/regression/issue845.rs
index 56037ae66..e8b0c0fd0 100644
--- a/vendor/serde_json/tests/regression/issue845.rs
+++ b/vendor/serde_json/tests/regression/issue845.rs
@@ -1,7 +1,6 @@
#![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;
diff --git a/vendor/serde_json/tests/test.rs b/vendor/serde_json/tests/test.rs
index 6c08cc8d2..8d9a5942a 100644
--- a/vendor/serde_json/tests/test.rs
+++ b/vendor/serde_json/tests/test.rs
@@ -53,7 +53,7 @@ macro_rules! treemap {
() => {
BTreeMap::new()
};
- ($($k:expr => $v:expr),+) => {
+ ($($k:expr => $v:expr),+ $(,)?) => {
{
let mut m = BTreeMap::new();
$(
@@ -264,7 +264,7 @@ fn test_write_object() {
(
treemap!(
"a".to_string() => true,
- "b".to_string() => false
+ "b".to_string() => false,
),
"{\"a\":true,\"b\":false}",
),
@@ -275,7 +275,7 @@ fn test_write_object() {
treemap![
"a".to_string() => treemap![],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
"{\"a\":{},\"b\":{},\"c\":{}}",
),
@@ -284,10 +284,10 @@ fn test_write_object() {
"a".to_string() => treemap![
"a".to_string() => treemap!["a" => vec![1,2,3]],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
"{\"a\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"b\":{},\"c\":{}}",
),
@@ -297,9 +297,9 @@ fn test_write_object() {
"b".to_string() => treemap![
"a".to_string() => treemap!["a" => vec![1,2,3]],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
"{\"a\":{},\"b\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}},\"c\":{}}",
),
@@ -310,8 +310,8 @@ fn test_write_object() {
"c".to_string() => treemap![
"a".to_string() => treemap!["a" => vec![1,2,3]],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
- ]
+ "c".to_string() => treemap![],
+ ],
],
"{\"a\":{},\"b\":{},\"c\":{\"a\":{\"a\":[1,2,3]},\"b\":{},\"c\":{}}}",
),
@@ -324,7 +324,7 @@ fn test_write_object() {
treemap![
"a".to_string() => treemap![],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
pretty_str!({
"a": {},
@@ -337,10 +337,10 @@ fn test_write_object() {
"a".to_string() => treemap![
"a".to_string() => treemap!["a" => vec![1,2,3]],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
pretty_str!({
"a": {
@@ -364,9 +364,9 @@ fn test_write_object() {
"b".to_string() => treemap![
"a".to_string() => treemap!["a" => vec![1,2,3]],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
- "c".to_string() => treemap![]
+ "c".to_string() => treemap![],
],
pretty_str!({
"a": {},
@@ -391,8 +391,8 @@ fn test_write_object() {
"c".to_string() => treemap![
"a".to_string() => treemap!["a" => vec![1,2,3]],
"b".to_string() => treemap![],
- "c".to_string() => treemap![]
- ]
+ "c".to_string() => treemap![],
+ ],
],
pretty_str!({
"a": {},
@@ -423,7 +423,7 @@ fn test_write_object() {
(
treemap!(
"a".to_string() => true,
- "b".to_string() => false
+ "b".to_string() => false,
),
pretty_str!( {
"a": true,
@@ -1192,8 +1192,8 @@ fn test_parse_object() {
treemap!(
"a".to_string() => treemap!(
"b".to_string() => 3u64,
- "c".to_string() => 4
- )
+ "c".to_string() => 4,
+ ),
),
)]);
@@ -1369,7 +1369,7 @@ fn test_parse_enum() {
),
treemap!(
"a".to_string() => Animal::Dog,
- "b".to_string() => Animal::Frog("Henry".to_string(), vec![])
+ "b".to_string() => Animal::Frog("Henry".to_string(), vec![]),
),
)]);
}
@@ -1452,7 +1452,6 @@ fn test_serialize_seq_with_no_len() {
where
T: ser::Serialize,
{
- #[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
@@ -1479,7 +1478,6 @@ fn test_serialize_seq_with_no_len() {
formatter.write_str("array")
}
- #[inline]
fn visit_unit<E>(self) -> Result<MyVec<T>, E>
where
E: de::Error,
@@ -1487,7 +1485,6 @@ fn test_serialize_seq_with_no_len() {
Ok(MyVec(Vec::new()))
}
- #[inline]
fn visit_seq<V>(self, mut visitor: V) -> Result<MyVec<T>, V::Error>
where
V: de::SeqAccess<'de>,
@@ -1538,7 +1535,6 @@ fn test_serialize_map_with_no_len() {
K: ser::Serialize + Ord,
V: ser::Serialize,
{
- #[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
@@ -1566,7 +1562,6 @@ fn test_serialize_map_with_no_len() {
formatter.write_str("map")
}
- #[inline]
fn visit_unit<E>(self) -> Result<MyMap<K, V>, E>
where
E: de::Error,
@@ -1574,7 +1569,6 @@ fn test_serialize_map_with_no_len() {
Ok(MyMap(BTreeMap::new()))
}
- #[inline]
fn visit_map<Visitor>(self, mut visitor: Visitor) -> Result<MyMap<K, V>, Visitor::Error>
where
Visitor: de::MapAccess<'de>,
@@ -1664,7 +1658,7 @@ fn test_deserialize_from_stream() {
fn test_serialize_rejects_bool_keys() {
let map = treemap!(
true => 2,
- false => 4
+ false => 4,
);
let err = to_vec(&map).unwrap_err();
@@ -1676,7 +1670,7 @@ fn test_serialize_rejects_adt_keys() {
let map = treemap!(
Some("a") => 2,
Some("b") => 4,
- None => 6
+ None => 6,
);
let err = to_vec(&map).unwrap_err();
@@ -1890,23 +1884,41 @@ fn test_integer_key() {
// map with integer keys
let map = treemap!(
1 => 2,
- -1 => 6
+ -1 => 6,
);
let j = r#"{"-1":6,"1":2}"#;
test_encode_ok(&[(&map, j)]);
test_parse_ok(vec![(j, map)]);
- let j = r#"{"x":null}"#;
- test_parse_err::<BTreeMap<i32, ()>>(&[(
- j,
- "invalid type: string \"x\", expected i32 at line 1 column 4",
- )]);
+ test_parse_err::<BTreeMap<i32, ()>>(&[
+ (
+ r#"{"x":null}"#,
+ "invalid value: expected key to be a number in quotes at line 1 column 2",
+ ),
+ (
+ r#"{" 123":null}"#,
+ "invalid value: expected key to be a number in quotes at line 1 column 2",
+ ),
+ (r#"{"123 ":null}"#, "expected `\"` at line 1 column 6"),
+ ]);
+
+ let err = from_value::<BTreeMap<i32, ()>>(json!({" 123":null})).unwrap_err();
+ assert_eq!(
+ err.to_string(),
+ "invalid value: expected key to be a number in quotes",
+ );
+
+ let err = from_value::<BTreeMap<i32, ()>>(json!({"123 ":null})).unwrap_err();
+ assert_eq!(
+ err.to_string(),
+ "invalid value: expected key to be a number in quotes",
+ );
}
#[test]
fn test_integer128_key() {
let map = treemap! {
- 100000000000000000000000000000000000000u128 => ()
+ 100000000000000000000000000000000000000u128 => (),
};
let j = r#"{"100000000000000000000000000000000000000":null}"#;
assert_eq!(to_string(&map).unwrap(), j);
@@ -1914,20 +1926,95 @@ fn test_integer128_key() {
}
#[test]
-fn test_deny_float_key() {
- #[derive(Eq, PartialEq, Ord, PartialOrd)]
+fn test_float_key() {
+ #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone)]
struct Float;
impl Serialize for Float {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
- serializer.serialize_f32(1.0)
+ serializer.serialize_f32(1.23)
+ }
+ }
+ impl<'de> Deserialize<'de> for Float {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: de::Deserializer<'de>,
+ {
+ f32::deserialize(deserializer).map(|_| Float)
}
}
// map with float key
- let map = treemap!(Float => "x");
+ let map = treemap!(Float => "x".to_owned());
+ let j = r#"{"1.23":"x"}"#;
+
+ test_encode_ok(&[(&map, j)]);
+ test_parse_ok(vec![(j, map)]);
+
+ let j = r#"{"x": null}"#;
+ test_parse_err::<BTreeMap<Float, ()>>(&[(
+ j,
+ "invalid value: expected key to be a number in quotes at line 1 column 2",
+ )]);
+}
+
+#[test]
+fn test_deny_non_finite_f32_key() {
+ // We store float bits so that we can derive Ord, and other traits. In a
+ // real context the code might involve a crate like ordered-float.
+
+ #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone)]
+ struct F32Bits(u32);
+ impl Serialize for F32Bits {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_f32(f32::from_bits(self.0))
+ }
+ }
+
+ let map = treemap!(F32Bits(f32::INFINITY.to_bits()) => "x".to_owned());
+ assert!(serde_json::to_string(&map).is_err());
+ assert!(serde_json::to_value(map).is_err());
+
+ let map = treemap!(F32Bits(f32::NEG_INFINITY.to_bits()) => "x".to_owned());
+ assert!(serde_json::to_string(&map).is_err());
+ assert!(serde_json::to_value(map).is_err());
+
+ let map = treemap!(F32Bits(f32::NAN.to_bits()) => "x".to_owned());
+ assert!(serde_json::to_string(&map).is_err());
+ assert!(serde_json::to_value(map).is_err());
+}
+
+#[test]
+fn test_deny_non_finite_f64_key() {
+ // We store float bits so that we can derive Ord, and other traits. In a
+ // real context the code might involve a crate like ordered-float.
+
+ #[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone)]
+ struct F64Bits(u64);
+ impl Serialize for F64Bits {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ serializer.serialize_f64(f64::from_bits(self.0))
+ }
+ }
+
+ let map = treemap!(F64Bits(f64::INFINITY.to_bits()) => "x".to_owned());
+ assert!(serde_json::to_string(&map).is_err());
+ assert!(serde_json::to_value(map).is_err());
+
+ let map = treemap!(F64Bits(f64::NEG_INFINITY.to_bits()) => "x".to_owned());
+ assert!(serde_json::to_string(&map).is_err());
+ assert!(serde_json::to_value(map).is_err());
+
+ let map = treemap!(F64Bits(f64::NAN.to_bits()) => "x".to_owned());
+ assert!(serde_json::to_string(&map).is_err());
assert!(serde_json::to_value(map).is_err());
}
@@ -1954,7 +2041,7 @@ fn test_effectively_string_keys() {
}
let map = treemap! {
Enum::One => 1,
- Enum::Two => 2
+ Enum::Two => 2,
};
let expected = r#"{"One":1,"Two":2}"#;
test_encode_ok(&[(&map, expected)]);
@@ -1964,7 +2051,7 @@ fn test_effectively_string_keys() {
struct Wrapper(String);
let map = treemap! {
Wrapper("zero".to_owned()) => 0,
- Wrapper("one".to_owned()) => 1
+ Wrapper("one".to_owned()) => 1,
};
let expected = r#"{"one":1,"zero":0}"#;
test_encode_ok(&[(&map, expected)]);
@@ -2385,6 +2472,12 @@ fn test_value_into_deserializer() {
let mut map = BTreeMap::new();
map.insert("inner", json!({ "string": "Hello World" }));
+ let outer = Outer::deserialize(serde::de::value::MapDeserializer::new(
+ map.iter().map(|(k, v)| (*k, v)),
+ ))
+ .unwrap();
+ assert_eq!(outer.inner.string, "Hello World");
+
let outer = Outer::deserialize(map.into_deserializer()).unwrap();
assert_eq!(outer.inner.string, "Hello World");
}