gel_protocol/model/json.rs
1/// A newtype for JSON received from the database
2#[derive(Debug, Clone, PartialEq)]
3#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
4pub struct Json(String);
5
6impl Json {
7    /// Create a JSON value without checking the contents.
8    ///
9    /// Two examples of use:
10    ///
11    /// 1) To construct values with the data received from the
12    ///    database, because we trust database to produce valid JSON.
13    ///
14    /// 2) By client users who are using data that is guaranteed
15    ///    to be valid JSON. If unsure, using a method such as serde_json's
16    ///    [to_string](https://docs.rs/serde_json/latest/serde_json/ser/fn.to_string.html)
17    ///    to construct a String is highly recommended.
18    ///
19    /// When used in a client query method, Gel itself will recognize if the
20    /// String inside `Json` is invalid JSON by returning `InvalidValueError:
21    /// invalid input syntax for type json`.
22    pub fn new_unchecked(value: String) -> Json {
23        Json(value)
24    }
25}
26
27impl AsRef<str> for Json {
28    fn as_ref(&self) -> &str {
29        &self.0
30    }
31}
32
33impl std::ops::Deref for Json {
34    type Target = str;
35    fn deref(&self) -> &str {
36        &self.0
37    }
38}
39
40impl From<Json> for String {
41    fn from(val: Json) -> Self {
42        val.0
43    }
44}