rocket_json_response/
to_json.rs

1use crate::json_gettext::{serde_json::Value, JSONGetTextValue};
2
3/// A data type that can be converted to a JSON-format string.
4pub trait ToJSON {
5    fn to_json(&self) -> String;
6}
7
8impl<'a> ToJSON for JSONGetTextValue<'a> {
9    #[inline]
10    fn to_json(&self) -> String {
11        self.to_json_string()
12    }
13}
14
15impl ToJSON for str {
16    #[inline]
17    fn to_json(&self) -> String {
18        JSONGetTextValue::from_str(self).to_json_string()
19    }
20}
21
22impl<'a> ToJSON for &'a str {
23    #[inline]
24    fn to_json(&self) -> String {
25        JSONGetTextValue::from_str(self).to_json_string()
26    }
27}
28
29impl ToJSON for String {
30    #[inline]
31    fn to_json(&self) -> String {
32        JSONGetTextValue::from_str(self).to_json_string()
33    }
34}
35
36impl ToJSON for bool {
37    #[inline]
38    fn to_json(&self) -> String {
39        JSONGetTextValue::from_bool(*self).to_json_string()
40    }
41}
42
43impl ToJSON for i8 {
44    #[inline]
45    fn to_json(&self) -> String {
46        JSONGetTextValue::from_i8(*self).to_json_string()
47    }
48}
49
50impl ToJSON for i16 {
51    #[inline]
52    fn to_json(&self) -> String {
53        JSONGetTextValue::from_i16(*self).to_json_string()
54    }
55}
56
57impl ToJSON for i32 {
58    #[inline]
59    fn to_json(&self) -> String {
60        JSONGetTextValue::from_i32(*self).to_json_string()
61    }
62}
63
64impl ToJSON for i64 {
65    #[inline]
66    fn to_json(&self) -> String {
67        JSONGetTextValue::from_i64(*self).to_json_string()
68    }
69}
70
71impl ToJSON for u8 {
72    #[inline]
73    fn to_json(&self) -> String {
74        JSONGetTextValue::from_u8(*self).to_json_string()
75    }
76}
77
78impl ToJSON for u16 {
79    #[inline]
80    fn to_json(&self) -> String {
81        JSONGetTextValue::from_u16(*self).to_json_string()
82    }
83}
84
85impl ToJSON for u32 {
86    #[inline]
87    fn to_json(&self) -> String {
88        JSONGetTextValue::from_u32(*self).to_json_string()
89    }
90}
91
92impl ToJSON for u64 {
93    #[inline]
94    fn to_json(&self) -> String {
95        JSONGetTextValue::from_u64(*self).to_json_string()
96    }
97}
98
99impl ToJSON for f32 {
100    #[inline]
101    fn to_json(&self) -> String {
102        JSONGetTextValue::from_f32(*self).to_json_string()
103    }
104}
105
106impl ToJSON for f64 {
107    #[inline]
108    fn to_json(&self) -> String {
109        JSONGetTextValue::from_f64(*self).to_json_string()
110    }
111}
112
113impl ToJSON for Value {
114    #[inline]
115    fn to_json(&self) -> String {
116        JSONGetTextValue::from_json_value_ref(self).to_json_string()
117    }
118}
119
120impl ToJSON for &Value {
121    #[inline]
122    fn to_json(&self) -> String {
123        JSONGetTextValue::from_json_value_ref(self).to_json_string()
124    }
125}
126
127impl<T: ToJSON> ToJSON for Option<T> {
128    #[inline]
129    fn to_json(&self) -> String {
130        match self {
131            Some(s) => s.to_json(),
132            None => JSONGetTextValue::null().to_json_string(),
133        }
134    }
135}
136
137#[macro_export]
138macro_rules! serialize_to_json {
139    ($t:ty) => {
140        impl $crate::ToJSON for $t {
141            #[inline]
142            fn to_json(&self) -> String {
143                $crate::json_gettext::serde_json::to_value(self).unwrap().to_json()
144            }
145        }
146    };
147}