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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::json_gettext::{serde_json::Value, JSONGetTextValue};

/// A data type that can be converted to a JSON-format string.
pub trait ToJSON {
    fn to_json(&self) -> String;
}

impl<'a> ToJSON for JSONGetTextValue<'a> {
    #[inline]
    fn to_json(&self) -> String {
        self.to_json_string()
    }
}

impl ToJSON for str {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_str(self).to_json_string()
    }
}

impl<'a> ToJSON for &'a str {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_str(self).to_json_string()
    }
}

impl ToJSON for String {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_str(self).to_json_string()
    }
}

impl ToJSON for bool {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_bool(*self).to_json_string()
    }
}

impl ToJSON for i8 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_i8(*self).to_json_string()
    }
}

impl ToJSON for i16 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_i16(*self).to_json_string()
    }
}

impl ToJSON for i32 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_i32(*self).to_json_string()
    }
}

impl ToJSON for i64 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_i64(*self).to_json_string()
    }
}

impl ToJSON for u8 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_u8(*self).to_json_string()
    }
}

impl ToJSON for u16 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_u16(*self).to_json_string()
    }
}

impl ToJSON for u32 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_u32(*self).to_json_string()
    }
}

impl ToJSON for u64 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_u64(*self).to_json_string()
    }
}

impl ToJSON for f32 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_f32(*self).to_json_string()
    }
}

impl ToJSON for f64 {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_f64(*self).to_json_string()
    }
}

impl ToJSON for Value {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_json_value_ref(self).to_json_string()
    }
}

impl ToJSON for &Value {
    #[inline]
    fn to_json(&self) -> String {
        JSONGetTextValue::from_json_value_ref(self).to_json_string()
    }
}

impl<T: ToJSON> ToJSON for Option<T> {
    #[inline]
    fn to_json(&self) -> String {
        match self {
            Some(s) => s.to_json(),
            None => JSONGetTextValue::null().to_json_string(),
        }
    }
}

#[macro_export]
macro_rules! serialize_to_json {
    ($t:ty) => {
        impl $crate::ToJSON for $t {
            #[inline]
            fn to_json(&self) -> String {
                $crate::json_gettext::serde_json::to_value(self).unwrap().to_json()
            }
        }
    };
}