diff --git a/ruma-events/src/room/pinned_events.rs b/ruma-events/src/room/pinned_events.rs index 91cd1807..8ec4d16a 100644 --- a/ruma-events/src/room/pinned_events.rs +++ b/ruma-events/src/room/pinned_events.rs @@ -34,7 +34,6 @@ mod tests { use ruma_identifiers::{EventId, RoomId, ServerName, UserId}; use ruma_serde::Raw; - use serde_json::to_string; use super::PinnedEventsEventContent; use crate::{StateEvent, Unsigned}; @@ -58,7 +57,7 @@ mod tests { unsigned: Unsigned::default(), }; - let serialized_event = to_string(&event).unwrap(); + let serialized_event = serde_json::to_string(&event).unwrap(); let parsed_event = serde_json::from_str::>>(&serialized_event) .unwrap() diff --git a/ruma-identifiers/src/device_key_id.rs b/ruma-identifiers/src/device_key_id.rs index 72bfc2b0..a5f29e26 100644 --- a/ruma-identifiers/src/device_key_id.rs +++ b/ruma-identifiers/src/device_key_id.rs @@ -60,9 +60,6 @@ common_impls!(DeviceKeyId, try_from, "Device key ID with algorithm and device ID mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; - use super::DeviceKeyId; use crate::{crypto_algorithms::DeviceKeyAlgorithm, Error}; @@ -80,16 +77,16 @@ mod tests { #[test] fn serialize_device_key_id() { let device_key_id = DeviceKeyId::try_from("ed25519:JLAFKJWSCS").unwrap(); - let serialized = to_json_value(device_key_id).unwrap(); + let serialized = serde_json::to_value(device_key_id).unwrap(); - let expected = json!("ed25519:JLAFKJWSCS"); - assert_eq!(serialized, expected); + assert_eq!(serialized, serde_json::json!("ed25519:JLAFKJWSCS")); } #[cfg(feature = "serde")] #[test] fn deserialize_device_key_id() { - let deserialized: DeviceKeyId = from_json_value(json!("ed25519:JLAFKJWSCS")).unwrap(); + let deserialized: DeviceKeyId = + serde_json::from_value(serde_json::json!("ed25519:JLAFKJWSCS")).unwrap(); let expected = DeviceKeyId::try_from("ed25519:JLAFKJWSCS").unwrap(); assert_eq!(deserialized, expected); diff --git a/ruma-identifiers/src/event_id.rs b/ruma-identifiers/src/event_id.rs index 5c212a42..1495eaa5 100644 --- a/ruma-identifiers/src/event_id.rs +++ b/ruma-identifiers/src/event_id.rs @@ -107,9 +107,6 @@ common_impls!(EventId, try_from, "a Matrix event ID"); mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_str, to_string}; - use super::EventId; use crate::Error; @@ -161,7 +158,7 @@ mod tests { #[test] fn serialize_valid_original_event_id() { assert_eq!( - to_string( + serde_json::to_string( &EventId::try_from("$39hvsi03hlne:example.com").expect("Failed to create EventId.") ) .expect("Failed to convert EventId to JSON."), @@ -173,7 +170,7 @@ mod tests { #[test] fn serialize_valid_base64_event_id() { assert_eq!( - to_string( + serde_json::to_string( &EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk") .expect("Failed to create EventId.") ) @@ -186,7 +183,7 @@ mod tests { #[test] fn serialize_valid_url_safe_base64_event_id() { assert_eq!( - to_string( + serde_json::to_string( &EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg") .expect("Failed to create EventId.") ) @@ -199,7 +196,7 @@ mod tests { #[test] fn deserialize_valid_original_event_id() { assert_eq!( - from_str::(r#""$39hvsi03hlne:example.com""#) + serde_json::from_str::(r#""$39hvsi03hlne:example.com""#) .expect("Failed to convert JSON to EventId"), EventId::try_from("$39hvsi03hlne:example.com").expect("Failed to create EventId.") ); @@ -209,7 +206,7 @@ mod tests { #[test] fn deserialize_valid_base64_event_id() { assert_eq!( - from_str::(r#""$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk""#) + serde_json::from_str::(r#""$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk""#) .expect("Failed to convert JSON to EventId"), EventId::try_from("$acR1l0raoZnm60CBwAVgqbZqoO/mYU81xysh1u7XcJk") .expect("Failed to create EventId.") @@ -220,7 +217,7 @@ mod tests { #[test] fn deserialize_valid_url_safe_base64_event_id() { assert_eq!( - from_str::(r#""$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg""#) + serde_json::from_str::(r#""$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg""#) .expect("Failed to convert JSON to EventId"), EventId::try_from("$Rqnc-F-dvnEYJTyHq_iKxU2bZ1CI92-kuZq3a5lr5Zg") .expect("Failed to create EventId.") diff --git a/ruma-identifiers/src/room_alias_id.rs b/ruma-identifiers/src/room_alias_id.rs index e6278943..1d7ae210 100644 --- a/ruma-identifiers/src/room_alias_id.rs +++ b/ruma-identifiers/src/room_alias_id.rs @@ -58,9 +58,6 @@ common_impls!(RoomAliasId, try_from, "a Matrix room alias ID"); mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_str, to_string}; - use super::RoomAliasId; use crate::Error; @@ -88,7 +85,7 @@ mod tests { #[test] fn serialize_valid_room_alias_id() { assert_eq!( - to_string( + serde_json::to_string( &RoomAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.") ) .expect("Failed to convert RoomAliasId to JSON."), @@ -100,7 +97,7 @@ mod tests { #[test] fn deserialize_valid_room_alias_id() { assert_eq!( - from_str::(r##""#ruma:example.com""##) + serde_json::from_str::(r##""#ruma:example.com""##) .expect("Failed to convert JSON to RoomAliasId"), RoomAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.") ); diff --git a/ruma-identifiers/src/room_id.rs b/ruma-identifiers/src/room_id.rs index 85c35315..91fe58cc 100644 --- a/ruma-identifiers/src/room_id.rs +++ b/ruma-identifiers/src/room_id.rs @@ -74,9 +74,6 @@ common_impls!(RoomId, try_from, "a Matrix room ID"); mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_str, to_string}; - use super::RoomId; use crate::Error; @@ -116,7 +113,7 @@ mod tests { #[test] fn serialize_valid_room_id() { assert_eq!( - to_string( + serde_json::to_string( &RoomId::try_from("!29fhd83h92h0:example.com").expect("Failed to create RoomId.") ) .expect("Failed to convert RoomId to JSON."), @@ -128,7 +125,7 @@ mod tests { #[test] fn deserialize_valid_room_id() { assert_eq!( - from_str::(r#""!29fhd83h92h0:example.com""#) + serde_json::from_str::(r#""!29fhd83h92h0:example.com""#) .expect("Failed to convert JSON to RoomId"), RoomId::try_from("!29fhd83h92h0:example.com").expect("Failed to create RoomId.") ); diff --git a/ruma-identifiers/src/room_id_or_room_alias_id.rs b/ruma-identifiers/src/room_id_or_room_alias_id.rs index b4d62e89..494c393f 100644 --- a/ruma-identifiers/src/room_id_or_room_alias_id.rs +++ b/ruma-identifiers/src/room_id_or_room_alias_id.rs @@ -144,9 +144,6 @@ impl TryFrom for RoomAliasId { mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_str, to_string}; - use super::RoomIdOrAliasId; use crate::Error; @@ -182,7 +179,7 @@ mod tests { #[test] fn serialize_valid_room_id_or_alias_id_with_a_room_alias_id() { assert_eq!( - to_string( + serde_json::to_string( &RoomIdOrAliasId::try_from("#ruma:example.com") .expect("Failed to create RoomAliasId.") ) @@ -195,7 +192,7 @@ mod tests { #[test] fn serialize_valid_room_id_or_alias_id_with_a_room_id() { assert_eq!( - to_string( + serde_json::to_string( &RoomIdOrAliasId::try_from("!29fhd83h92h0:example.com") .expect("Failed to create RoomId.") ) @@ -208,7 +205,7 @@ mod tests { #[test] fn deserialize_valid_room_id_or_alias_id_with_a_room_alias_id() { assert_eq!( - from_str::(r##""#ruma:example.com""##) + serde_json::from_str::(r##""#ruma:example.com""##) .expect("Failed to convert JSON to RoomAliasId"), RoomIdOrAliasId::try_from("#ruma:example.com").expect("Failed to create RoomAliasId.") ); @@ -218,7 +215,7 @@ mod tests { #[test] fn deserialize_valid_room_id_or_alias_id_with_a_room_id() { assert_eq!( - from_str::(r##""!29fhd83h92h0:example.com""##) + serde_json::from_str::(r##""!29fhd83h92h0:example.com""##) .expect("Failed to convert JSON to RoomId"), RoomIdOrAliasId::try_from("!29fhd83h92h0:example.com") .expect("Failed to create RoomAliasId.") diff --git a/ruma-identifiers/src/room_version_id.rs b/ruma-identifiers/src/room_version_id.rs index 221900ce..5b9e2955 100644 --- a/ruma-identifiers/src/room_version_id.rs +++ b/ruma-identifiers/src/room_version_id.rs @@ -230,9 +230,6 @@ impl AsRef for CustomRoomVersion { mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_str, to_string}; - use super::RoomVersionId; use crate::Error; @@ -309,8 +306,10 @@ mod tests { #[test] fn serialize_official_room_id() { assert_eq!( - to_string(&RoomVersionId::try_from("1").expect("Failed to create RoomVersionId.")) - .expect("Failed to convert RoomVersionId to JSON."), + serde_json::to_string( + &RoomVersionId::try_from("1").expect("Failed to create RoomVersionId.") + ) + .expect("Failed to convert RoomVersionId to JSON."), r#""1""# ); } @@ -318,8 +317,8 @@ mod tests { #[cfg(feature = "serde")] #[test] fn deserialize_official_room_id() { - let deserialized = - from_str::(r#""1""#).expect("Failed to convert RoomVersionId to JSON."); + let deserialized = serde_json::from_str::(r#""1""#) + .expect("Failed to convert RoomVersionId to JSON."); assert_eq!(deserialized, RoomVersionId::Version1); @@ -333,7 +332,7 @@ mod tests { #[test] fn serialize_custom_room_id() { assert_eq!( - to_string( + serde_json::to_string( &RoomVersionId::try_from("io.ruma.1").expect("Failed to create RoomVersionId.") ) .expect("Failed to convert RoomVersionId to JSON."), @@ -344,7 +343,7 @@ mod tests { #[cfg(feature = "serde")] #[test] fn deserialize_custom_room_id() { - let deserialized = from_str::(r#""io.ruma.1""#) + let deserialized = serde_json::from_str::(r#""io.ruma.1""#) .expect("Failed to convert RoomVersionId to JSON."); assert_eq!( diff --git a/ruma-identifiers/src/user_id.rs b/ruma-identifiers/src/user_id.rs index 1ba0a033..10e885ae 100644 --- a/ruma-identifiers/src/user_id.rs +++ b/ruma-identifiers/src/user_id.rs @@ -115,9 +115,6 @@ pub use ruma_identifiers_validation::user_id::localpart_is_fully_comforming; mod tests { use std::convert::TryFrom; - #[cfg(feature = "serde")] - use serde_json::{from_str, to_string}; - use super::UserId; use crate::{Error, ServerName}; @@ -208,8 +205,10 @@ mod tests { #[test] fn serialize_valid_user_id() { assert_eq!( - to_string(&UserId::try_from("@carl:example.com").expect("Failed to create UserId.")) - .expect("Failed to convert UserId to JSON."), + serde_json::to_string( + &UserId::try_from("@carl:example.com").expect("Failed to create UserId.") + ) + .expect("Failed to convert UserId to JSON."), r#""@carl:example.com""# ); } @@ -218,7 +217,8 @@ mod tests { #[test] fn deserialize_valid_user_id() { assert_eq!( - from_str::(r#""@carl:example.com""#).expect("Failed to convert JSON to UserId"), + serde_json::from_str::(r#""@carl:example.com""#) + .expect("Failed to convert JSON to UserId"), UserId::try_from("@carl:example.com").expect("Failed to create UserId.") ); } diff --git a/ruma-signatures/src/lib.rs b/ruma-signatures/src/lib.rs index 20ed065f..d839fea6 100644 --- a/ruma-signatures/src/lib.rs +++ b/ruma-signatures/src/lib.rs @@ -175,7 +175,7 @@ mod tests { use base64::{decode_config, STANDARD_NO_PAD}; use ring::signature::{Ed25519KeyPair as RingEd25519KeyPair, KeyPair as _}; use ruma_identifiers::RoomVersionId; - use serde_json::{from_str, to_string}; + use serde_json::{from_str as from_json_str, to_string as to_json_string}; use super::{ canonical_json, hash_and_sign_event, sign_json, verify_event, verify_json, Ed25519KeyPair, @@ -200,7 +200,7 @@ mod tests { /// Convenience for converting a string of JSON into its canonical form. fn test_canonical_json(input: &str) -> String { - let object = from_str(input).unwrap(); + let object = from_json_str(input).unwrap(); canonical_json(&object) } @@ -301,19 +301,19 @@ mod tests { ) .unwrap(); - let mut value = from_str("{}").unwrap(); + let mut value = from_json_str("{}").unwrap(); sign_json("domain", &key_pair, &mut value).unwrap(); assert_eq!( - to_string(&value).unwrap(), + to_json_string(&value).unwrap(), r#"{"signatures":{"domain":{"ed25519:1":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}}"# ); } #[test] fn verify_empty_json() { - let value = from_str(r#"{"signatures":{"domain":{"ed25519:1":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}}"#).unwrap(); + let value = from_json_str(r#"{"signatures":{"domain":{"ed25519:1":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}}"#).unwrap(); let mut signature_set = BTreeMap::new(); signature_set.insert("ed25519:1".into(), public_key_string()); @@ -332,27 +332,27 @@ mod tests { ) .unwrap(); - let mut alpha_object = from_str(r#"{ "one": 1, "two": "Two" }"#).unwrap(); + let mut alpha_object = from_json_str(r#"{ "one": 1, "two": "Two" }"#).unwrap(); sign_json("domain", &key_pair, &mut alpha_object).unwrap(); assert_eq!( - to_string(&alpha_object).unwrap(), + to_json_string(&alpha_object).unwrap(), r#"{"one":1,"signatures":{"domain":{"ed25519:1":"t6Ehmh6XTDz7qNWI0QI5tNPSliWLPQP/+Fzz3LpdCS7q1k2G2/5b5Embs2j4uG3ZeivejrzqSVoBcdocRpa+AQ"}},"two":"Two"}"# ); let mut reverse_alpha_object = - from_str(r#"{ "two": "Two", "one": 1 }"#).expect("reverse_alpha should serialize"); + from_json_str(r#"{ "two": "Two", "one": 1 }"#).expect("reverse_alpha should serialize"); sign_json("domain", &key_pair, &mut reverse_alpha_object).unwrap(); assert_eq!( - to_string(&reverse_alpha_object).unwrap(), + to_json_string(&reverse_alpha_object).unwrap(), r#"{"one":1,"signatures":{"domain":{"ed25519:1":"t6Ehmh6XTDz7qNWI0QI5tNPSliWLPQP/+Fzz3LpdCS7q1k2G2/5b5Embs2j4uG3ZeivejrzqSVoBcdocRpa+AQ"}},"two":"Two"}"# ); } #[test] fn verify_minimal_json() { - let value = from_str( + let value = from_json_str( r#"{"one":1,"signatures":{"domain":{"ed25519:1":"t6Ehmh6XTDz7qNWI0QI5tNPSliWLPQP/+Fzz3LpdCS7q1k2G2/5b5Embs2j4uG3ZeivejrzqSVoBcdocRpa+AQ"}},"two":"Two"}"# ).unwrap(); @@ -364,7 +364,7 @@ mod tests { assert!(verify_json(&public_key_map, &value).is_ok()); - let reverse_value = from_str( + let reverse_value = from_json_str( r#"{"two":"Two","signatures":{"domain":{"ed25519:1":"t6Ehmh6XTDz7qNWI0QI5tNPSliWLPQP/+Fzz3LpdCS7q1k2G2/5b5Embs2j4uG3ZeivejrzqSVoBcdocRpa+AQ"}},"one":1}"# ).unwrap(); @@ -373,7 +373,7 @@ mod tests { #[test] fn fail_verify_json() { - let value = from_str(r#"{"not":"empty","signatures":{"domain":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}"#).unwrap(); + let value = from_json_str(r#"{"not":"empty","signatures":{"domain":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}"#).unwrap(); let mut signature_set = BTreeMap::new(); signature_set.insert("ed25519:1".into(), public_key_string()); @@ -409,11 +409,11 @@ mod tests { } }"#; - let mut object = from_str(json).unwrap(); + let mut object = from_json_str(json).unwrap(); hash_and_sign_event("domain", &key_pair, &mut object, &RoomVersionId::Version5).unwrap(); assert_eq!( - to_string(&object).unwrap(), + to_json_string(&object).unwrap(), r#"{"auth_events":[],"content":{},"depth":3,"hashes":{"sha256":"5jM4wQpv6lnBo7CLIghJuHdW+s2CMBJPUOGOC89ncos"},"origin":"domain","origin_server_ts":1000000,"prev_events":[],"room_id":"!x:domain","sender":"@a:domain","signatures":{"domain":{"ed25519:1":"PxOFMn6ORll8PFSQp0IRF6037MEZt3Mfzu/ROiT/gb/ccs1G+f6Ddoswez4KntLPBI3GKCGIkhctiK37JOy2Aw"}},"type":"X","unsigned":{"age_ts":1000000}}"# ); } @@ -442,11 +442,11 @@ mod tests { } }"#; - let mut object = from_str(json).unwrap(); + let mut object = from_json_str(json).unwrap(); hash_and_sign_event("domain", &key_pair, &mut object, &RoomVersionId::Version5).unwrap(); assert_eq!( - to_string(&object).unwrap(), + to_json_string(&object).unwrap(), r#"{"content":{"body":"Here is the message content"},"event_id":"$0:domain","hashes":{"sha256":"onLKD1bGljeBWQhWZ1kaP9SorVmRQNdN5aM2JYU2n/g"},"origin":"domain","origin_server_ts":1000000,"room_id":"!r:domain","sender":"@u:domain","signatures":{"domain":{"ed25519:1":"D2V+qWBJssVuK/pEUJtwaYMdww2q1fP4PRCo226ChlLz8u8AWmQdLKes19NMjs/X0Hv0HIjU0c1TDKFMtGuoCA"}},"type":"m.room.message","unsigned":{"age_ts":1000000}}"# ); } @@ -459,7 +459,7 @@ mod tests { let mut public_key_map = BTreeMap::new(); public_key_map.insert("domain".into(), signature_set); - let value = from_str( + let value = from_json_str( r#"{ "auth_events": [], "content": {},