pub struct Json<T>(pub T)
where
T: ?Sized;
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<Json<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Json<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceยงimpl<T> Encode<'_, MySql> for Json<T>where
T: Serialize,
impl<T> Encode<'_, MySql> for Json<T>where
T: Serialize,
sourceยงfn encode_by_ref(
&self,
buf: &mut Vec<u8>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn encode_by_ref( &self, buf: &mut Vec<u8>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
sourceยงfn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
Writes the value of
self
into buf
in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
sourceยงimpl<T> Encode<'_, Sqlite> for Json<T>where
T: Serialize,
impl<T> Encode<'_, Sqlite> for Json<T>where
T: Serialize,
sourceยงfn encode_by_ref(
&self,
buf: &mut Vec<SqliteArgumentValue<'_>>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn encode_by_ref( &self, buf: &mut Vec<SqliteArgumentValue<'_>>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
sourceยงfn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
Writes the value of
self
into buf
in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
sourceยงimpl<'q, T> Encode<'q, Postgres> for Json<T>where
T: Serialize,
impl<'q, T> Encode<'q, Postgres> for Json<T>where
T: Serialize,
sourceยงfn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
sourceยงfn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
Writes the value of
self
into buf
in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
fn size_hint(&self) -> usize
sourceยงimpl<T> PartialOrd for Json<T>where
T: PartialOrd + ?Sized,
impl<T> PartialOrd for Json<T>where
T: PartialOrd + ?Sized,
sourceยงimpl<T> PgHasArrayType for Json<T>
impl<T> PgHasArrayType for Json<T>
fn array_type_info() -> PgTypeInfo
fn array_compatible(ty: &PgTypeInfo) -> bool
sourceยงimpl<T> Serialize for Json<T>
impl<T> Serialize for Json<T>
sourceยงfn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. Read more
sourceยงimpl<T> Type<MySql> for Json<T>
impl<T> Type<MySql> for Json<T>
sourceยงfn type_info() -> MySqlTypeInfo
fn type_info() -> MySqlTypeInfo
Returns the canonical SQL type for this Rust type. Read more
sourceยงfn compatible(ty: &MySqlTypeInfo) -> bool
fn compatible(ty: &MySqlTypeInfo) -> bool
Determines if this Rust type is compatible with the given SQL type. Read more
sourceยงimpl<T> Type<Postgres> for Json<T>
impl<T> Type<Postgres> for Json<T>
sourceยงfn type_info() -> PgTypeInfo
fn type_info() -> PgTypeInfo
Returns the canonical SQL type for this Rust type. Read more
sourceยงfn compatible(ty: &PgTypeInfo) -> bool
fn compatible(ty: &PgTypeInfo) -> bool
Determines if this Rust type is compatible with the given SQL type. Read more
sourceยงimpl<T> Type<Sqlite> for Json<T>
impl<T> Type<Sqlite> for Json<T>
sourceยงfn type_info() -> SqliteTypeInfo
fn type_info() -> SqliteTypeInfo
Returns the canonical SQL type for this Rust type. Read more
sourceยงfn compatible(ty: &SqliteTypeInfo) -> bool
fn compatible(ty: &SqliteTypeInfo) -> bool
Determines if this Rust type is compatible with the given SQL type. Read more
impl<T> Copy for Json<T>
impl<T> Eq for Json<T>
impl<T> StructuralPartialEq for Json<T>where
T: ?Sized,
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