Add example for canonical_json.

This commit is contained in:
Jimmy Cuadra 2019-07-12 04:10:44 -07:00
parent 31d5c42a75
commit 798a2387cf
2 changed files with 29 additions and 22 deletions

View File

@ -171,6 +171,22 @@ where
/// # Errors
///
/// Returns an error if the provided JSON value is not a JSON object.
///
/// # Examples
///
/// ```rust
/// let input =
/// r#"{
/// "本": 2,
/// "日": 1
/// }"#;
///
/// let value = serde_json::from_str::<serde_json::Value>(input).unwrap();
///
/// let canonical = ruma_signatures::canonical_json(&value).unwrap();
///
/// assert_eq!(canonical, r#"{"日":1,"本":2}"#);
/// ```
pub fn canonical_json(value: &Value) -> Result<String, Error> {
canonical_json_with_fields_to_remove(value, CANONICAL_JSON_FIELDS_TO_REMOVE)
}
@ -195,15 +211,6 @@ pub fn canonical_json(value: &Value) -> Result<String, Error> {
/// use std::collections::HashMap;
///
/// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
/// const SIGNATURE_BYTES: &str =
/// "K8280/U9SSy9IVtjBuVeLr+HpOB4BQFWbg+UZaADMtTdGYI7Geitb76LTrr5QV/7Xg4ahLwYGYZzuHGZKM5ZAQ";
///
/// // Decode the public key used to generate the signature into raw bytes.
/// let public_key = base64::decode_config(&PUBLIC_KEY, base64::STANDARD_NO_PAD).unwrap();
///
/// // Create a `Signature` from the raw bytes of the signature.
/// let signature_bytes = base64::decode_config(&SIGNATURE_BYTES, base64::STANDARD_NO_PAD).unwrap();
/// let signature = ruma_signatures::Signature::new("ed25519:1", &signature_bytes).unwrap();
///
/// // Deserialize the signed JSON.
/// let value = serde_json::from_str(

View File

@ -223,9 +223,9 @@ mod test {
assert_eq!(
&test_canonical_json(
r#"{
"one": 1,
"two": "Two"
}"#
"one": 1,
"two": "Two"
}"#
),
r#"{"one":1,"two":"Two"}"#
);
@ -233,9 +233,9 @@ mod test {
assert_eq!(
&test_canonical_json(
r#"{
"b": "2",
"a": "1"
}"#
"b": "2",
"a": "1"
}"#
),
r#"{"a":"1","b":"2"}"#
);
@ -271,8 +271,8 @@ mod test {
assert_eq!(
&test_canonical_json(
r#"{
"a": "日本語"
}"#
"a": "日本語"
}"#
),
r#"{"a":"日本語"}"#
);
@ -280,9 +280,9 @@ mod test {
assert_eq!(
&test_canonical_json(
r#"{
"": 2,
"": 1
}"#
"": 2,
"": 1
}"#
),
r#"{"日":1,"本":2}"#
);
@ -290,8 +290,8 @@ mod test {
assert_eq!(
&test_canonical_json(
r#"{
"a": "\u65E5"
}"#
"a": "\u65E5"
}"#
),
r#"{"a":"日"}"#
);