-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathjson.rs
49 lines (41 loc) · 947 Bytes
/
json.rs
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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
use serde_json::Value as Json;
use std::borrow::Cow;
pub trait ToJson {
fn to_json(&self) -> Json;
}
impl ToJson for Json {
#[track_caller]
fn to_json(&self) -> Json {
self.clone()
}
}
impl ToJson for Cow<'_, str> {
#[track_caller]
fn to_json(&self) -> Json {
self.as_ref().to_json()
}
}
impl ToJson for String {
#[track_caller]
fn to_json(&self) -> Json {
self.as_str().to_json()
}
}
impl ToJson for &str {
#[track_caller]
fn to_json(&self) -> Json {
serde_json::from_str(self).expect("Error on deserializing JSON from string")
}
}
#[macro_export]
macro_rules! assert_eq_json {
($left:expr, $right:expr) => {{
use $crate::test_utils::json::ToJson;
let left = $left.to_json();
let right = $right.to_json();
assert_eq!(left, right);
}};
}