pub struct Json<T: ?Sized>(pub T);
Expand description
Json for json and jsonb fields
Will attempt to cast to type passed in as the generic.
[dependencies]
serde_json = { version = "1.0", features = ["raw_value"] }
§Example
#[derive(Deserialize)]
struct Book {
name: String
}
#[derive(sqlx::FromRow)]
struct Author {
name: String,
books: sqlx::types::Json<Book>
}
Can also be used to turn the json/jsonb into a hashmap
use std::collections::HashMap;
use serde::Deserialize;
#[derive(Deserialize)]
struct Book {
name: String
}
#[derive(sqlx::FromRow)]
struct Library {
id: String,
dewey_decimal: sqlx::types::Json<HashMap<String, Book>>
}
If the query macros are used, it is necessary to tell the macro to use
the Json
adapter by using the type override syntax
ⓘ
#[derive(sqlx::FromRow)]
struct Book {
title: String,
}
#[derive(sqlx::FromRow)]
struct Author {
name: String,
books: sqlx::types::Json<Book>,
}
// Note the type override in the query string
let authors = sqlx::query_as!(
Author,
r#"
SELECT name, books as "books: Json<Book>"
FROM authors
"#
)
.fetch_all(&mut conn)
.await?;
Tuple Fields§
§0: T
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for Json<T>where
T: Deserialize<'de> + ?Sized,
impl<'de, T> Deserialize<'de> for Json<T>where
T: Deserialize<'de> + ?Sized,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl<T: PartialOrd + ?Sized> PartialOrd for Json<T>
impl<T: PartialOrd + ?Sized> PartialOrd for Json<T>
impl<T: Copy + ?Sized> Copy for Json<T>
impl<T: Eq + ?Sized> Eq for Json<T>
impl<T: ?Sized> StructuralPartialEq for Json<T>
Auto Trait Implementations§
impl<T> Freeze for Json<T>
impl<T> RefUnwindSafe for Json<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for Json<T>
impl<T> Sync for Json<T>
impl<T> Unpin for Json<T>
impl<T> UnwindSafe for Json<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more