1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/// Asserts that the canonical JSON serialization of a type is equal to an expected JSON value.
///
/// By calling [`to_canonical_value()`] internally this macro enforces strict serialization rules
/// which allows to avoid some pitfalls like having a [`serde::Serialize`] implementation that
/// generates the same key twice in an object, which is ignored by [`serde_json::to_value()`].
///
/// The expression on the left is the one whose serialization needs to be checked. It must
/// implement [`serde::Serialize`], and is serialized using [`to_canonical_value()`].
///
/// The expression on the right is the expected JSON serialization as a [`serde_json::Value`]. It is
/// usually a declaration using the [`serde_json::json!`] macro. It is then converted to a
/// [`CanonicalJsonValue`] and compared with the result of the serialization of the expression on
/// the left.
///
/// Panics if the expression on the left fails to serialize, if the expected JSON fails to be
/// converted to a [`CanonicalJsonValue`], or if the two values are not equal.
///
/// This macro will print the error if the serialization or the conversion fails, or both debug
/// representations of the [`CanonicalJsonValue`]s if they are not equal.
///
/// ## Example
///
/// ```
/// use ruma_common::canonical_json::assert_to_canonical_json_eq;
/// use serde::Serialize;
/// use serde_json::json;
///
/// #[derive(Serialize)]
/// struct Data {
/// id: String,
/// flag: bool,
/// }
///
/// let data = Data { id: "abcdef".to_owned(), flag: true };
/// assert_to_canonical_json_eq!(data, json!({ "id": "abcdef", "flag": true }));
/// ```
///
/// [`CanonicalJsonValue`]: super::CanonicalJsonValue
/// [`to_canonical_value()`]: super::to_canonical_value