sqlx_core/types/
json.rs

1use std::ops::{Deref, DerefMut};
2
3use serde::{Deserialize, Serialize};
4pub use serde_json::value::RawValue as JsonRawValue;
5pub use serde_json::Value as JsonValue;
6
7use crate::database::Database;
8use crate::decode::Decode;
9use crate::encode::{Encode, IsNull};
10use crate::error::BoxDynError;
11use crate::types::Type;
12
13/// Json for json and jsonb fields
14///
15/// Will attempt to cast to type passed in as the generic.
16///
17/// ```toml
18/// [dependencies]
19/// serde_json = { version = "1.0", features = ["raw_value"] }
20///
21/// ```
22///
23/// # Example
24///
25/// ```
26/// # use serde::Deserialize;
27/// #[derive(Deserialize)]
28/// struct Book {
29///   name: String
30/// }
31///
32/// #[derive(sqlx::FromRow)]
33/// struct Author {
34///   name: String,
35///   books: sqlx::types::Json<Book>
36/// }
37/// ```
38///
39/// Can also be used to turn the json/jsonb into a hashmap
40/// ```
41/// use std::collections::HashMap;
42/// use serde::Deserialize;
43///
44/// #[derive(Deserialize)]
45/// struct Book {
46///   name: String
47/// }
48/// #[derive(sqlx::FromRow)]
49/// struct Library {
50///   id: String,
51///   dewey_decimal: sqlx::types::Json<HashMap<String, Book>>
52/// }
53/// ```
54///
55/// If the query macros are used, it is necessary to tell the macro to use
56/// the `Json` adapter by using the type override syntax
57/// ```rust,ignore
58/// # async fn example3() -> sqlx::Result<()> {
59/// # let mut conn: sqlx::PgConnection = unimplemented!();
60/// #[derive(sqlx::FromRow)]
61/// struct Book {
62///     title: String,
63/// }
64///
65/// #[derive(sqlx::FromRow)]
66/// struct Author {
67///     name: String,
68///     books: sqlx::types::Json<Book>,
69/// }
70/// // Note the type override in the query string
71/// let authors = sqlx::query_as!(
72///     Author,
73///     r#"
74/// SELECT name, books as "books: Json<Book>"
75/// FROM authors
76///     "#
77/// )
78/// .fetch_all(&mut conn)
79/// .await?;
80/// # Ok(())
81/// # }
82/// ```
83#[derive(
84    Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
85)]
86#[serde(transparent)]
87pub struct Json<T: ?Sized>(pub T);
88
89impl<T> From<T> for Json<T> {
90    fn from(value: T) -> Self {
91        Self(value)
92    }
93}
94
95impl<T> Deref for Json<T> {
96    type Target = T;
97
98    fn deref(&self) -> &Self::Target {
99        &self.0
100    }
101}
102
103impl<T> DerefMut for Json<T> {
104    fn deref_mut(&mut self) -> &mut Self::Target {
105        &mut self.0
106    }
107}
108
109impl<T> AsRef<T> for Json<T> {
110    fn as_ref(&self) -> &T {
111        &self.0
112    }
113}
114
115impl<T> AsMut<T> for Json<T> {
116    fn as_mut(&mut self) -> &mut T {
117        &mut self.0
118    }
119}
120
121// UNSTABLE: for driver use only!
122#[doc(hidden)]
123impl<T: Serialize> Json<T> {
124    pub fn encode_to_string(&self) -> Result<String, serde_json::Error> {
125        serde_json::to_string(self)
126    }
127
128    pub fn encode_to(&self, buf: &mut Vec<u8>) -> Result<(), serde_json::Error> {
129        serde_json::to_writer(buf, self)
130    }
131}
132
133// UNSTABLE: for driver use only!
134#[doc(hidden)]
135impl<'a, T: 'a> Json<T>
136where
137    T: Deserialize<'a>,
138{
139    pub fn decode_from_string(s: &'a str) -> Result<Self, BoxDynError> {
140        serde_json::from_str(s).map_err(Into::into)
141    }
142
143    pub fn decode_from_bytes(bytes: &'a [u8]) -> Result<Self, BoxDynError> {
144        serde_json::from_slice(bytes).map_err(Into::into)
145    }
146}
147
148impl<DB> Type<DB> for JsonValue
149where
150    Json<Self>: Type<DB>,
151    DB: Database,
152{
153    fn type_info() -> DB::TypeInfo {
154        <Json<Self> as Type<DB>>::type_info()
155    }
156
157    fn compatible(ty: &DB::TypeInfo) -> bool {
158        <Json<Self> as Type<DB>>::compatible(ty)
159    }
160}
161
162impl<'q, DB> Encode<'q, DB> for JsonValue
163where
164    for<'a> Json<&'a Self>: Encode<'q, DB>,
165    DB: Database,
166{
167    fn encode_by_ref(
168        &self,
169        buf: &mut <DB as Database>::ArgumentBuffer<'q>,
170    ) -> Result<IsNull, BoxDynError> {
171        <Json<&Self> as Encode<'q, DB>>::encode(Json(self), buf)
172    }
173}
174
175impl<'r, DB> Decode<'r, DB> for JsonValue
176where
177    Json<Self>: Decode<'r, DB>,
178    DB: Database,
179{
180    fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
181        <Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
182    }
183}
184
185impl<DB> Type<DB> for JsonRawValue
186where
187    for<'a> Json<&'a Self>: Type<DB>,
188    DB: Database,
189{
190    fn type_info() -> DB::TypeInfo {
191        <Json<&Self> as Type<DB>>::type_info()
192    }
193
194    fn compatible(ty: &DB::TypeInfo) -> bool {
195        <Json<&Self> as Type<DB>>::compatible(ty)
196    }
197}
198
199// We don't have to implement Encode for JsonRawValue because that's covered by the default
200// implementation for Encode
201impl<'r, DB> Decode<'r, DB> for &'r JsonRawValue
202where
203    Json<Self>: Decode<'r, DB>,
204    DB: Database,
205{
206    fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
207        <Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
208    }
209}