Add example for canonical_json
.
This commit is contained in:
parent
31d5c42a75
commit
798a2387cf
@ -171,6 +171,22 @@ where
|
|||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns an error if the provided JSON value is not a JSON object.
|
/// 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> {
|
pub fn canonical_json(value: &Value) -> Result<String, Error> {
|
||||||
canonical_json_with_fields_to_remove(value, CANONICAL_JSON_FIELDS_TO_REMOVE)
|
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;
|
/// use std::collections::HashMap;
|
||||||
///
|
///
|
||||||
/// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI";
|
/// 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.
|
/// // Deserialize the signed JSON.
|
||||||
/// let value = serde_json::from_str(
|
/// let value = serde_json::from_str(
|
||||||
|
26
src/lib.rs
26
src/lib.rs
@ -223,9 +223,9 @@ mod test {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
&test_canonical_json(
|
&test_canonical_json(
|
||||||
r#"{
|
r#"{
|
||||||
"one": 1,
|
"one": 1,
|
||||||
"two": "Two"
|
"two": "Two"
|
||||||
}"#
|
}"#
|
||||||
),
|
),
|
||||||
r#"{"one":1,"two":"Two"}"#
|
r#"{"one":1,"two":"Two"}"#
|
||||||
);
|
);
|
||||||
@ -233,9 +233,9 @@ mod test {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
&test_canonical_json(
|
&test_canonical_json(
|
||||||
r#"{
|
r#"{
|
||||||
"b": "2",
|
"b": "2",
|
||||||
"a": "1"
|
"a": "1"
|
||||||
}"#
|
}"#
|
||||||
),
|
),
|
||||||
r#"{"a":"1","b":"2"}"#
|
r#"{"a":"1","b":"2"}"#
|
||||||
);
|
);
|
||||||
@ -271,8 +271,8 @@ mod test {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
&test_canonical_json(
|
&test_canonical_json(
|
||||||
r#"{
|
r#"{
|
||||||
"a": "日本語"
|
"a": "日本語"
|
||||||
}"#
|
}"#
|
||||||
),
|
),
|
||||||
r#"{"a":"日本語"}"#
|
r#"{"a":"日本語"}"#
|
||||||
);
|
);
|
||||||
@ -280,9 +280,9 @@ mod test {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
&test_canonical_json(
|
&test_canonical_json(
|
||||||
r#"{
|
r#"{
|
||||||
"本": 2,
|
"本": 2,
|
||||||
"日": 1
|
"日": 1
|
||||||
}"#
|
}"#
|
||||||
),
|
),
|
||||||
r#"{"日":1,"本":2}"#
|
r#"{"日":1,"本":2}"#
|
||||||
);
|
);
|
||||||
@ -290,8 +290,8 @@ mod test {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
&test_canonical_json(
|
&test_canonical_json(
|
||||||
r#"{
|
r#"{
|
||||||
"a": "\u65E5"
|
"a": "\u65E5"
|
||||||
}"#
|
}"#
|
||||||
),
|
),
|
||||||
r#"{"a":"日"}"#
|
r#"{"a":"日"}"#
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user