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>>
}
Tuple Fields§
§0: T
Trait Implementations§
source§impl<'r, T> Decode<'r, MySql> for Json<T>where
T: 'r + Deserialize<'r>,
impl<'r, T> Decode<'r, MySql> for Json<T>where T: 'r + Deserialize<'r>,
source§fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError>
fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError>
Decode a new value of this type using a raw value from the database.
source§impl<'r, T> Decode<'r, Postgres> for Json<T>where
T: Deserialize<'r> + 'r,
impl<'r, T> Decode<'r, Postgres> for Json<T>where T: Deserialize<'r> + 'r,
source§fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>
fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError>
Decode a new value of this type using a raw value from the database.
source§impl<'r, T> Decode<'r, Sqlite> for Json<T>where
T: 'r + Deserialize<'r>,
impl<'r, T> Decode<'r, Sqlite> for Json<T>where T: 'r + Deserialize<'r>,
source§fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError>
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError>
Decode a new value of this type using a raw value from the database.
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> Encode<'_, MySql> for Json<T>where
T: Serialize,
impl<T> Encode<'_, MySql> for Json<T>where T: Serialize,
source§fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
Self: Sized,
fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere Self: Sized,
Writes the value of
self
into buf
in the expected format for the database.fn produces(&self) -> Option<DB::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<'_>>) -> IsNull
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull
source§fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
Self: Sized,
fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere Self: Sized,
Writes the value of
self
into buf
in the expected format for the database.fn produces(&self) -> Option<DB::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) -> IsNull
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull
source§fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere
Self: Sized,
fn encode(self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNullwhere Self: Sized,
Writes the value of
self
into buf
in the expected format for the database.fn produces(&self) -> Option<DB::TypeInfo>
fn size_hint(&self) -> usize
source§impl<T: PartialEq + ?Sized> PartialEq<Json<T>> for Json<T>
impl<T: PartialEq + ?Sized> PartialEq<Json<T>> for Json<T>
source§impl<T: PartialOrd + ?Sized> PartialOrd<Json<T>> for Json<T>
impl<T: PartialOrd + ?Sized> PartialOrd<Json<T>> for Json<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§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> 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 + ?Sized> Copy for Json<T>
impl<T: Eq + ?Sized> Eq for Json<T>
impl<T: ?Sized> StructuralEq for Json<T>
impl<T: ?Sized> StructuralPartialEq for Json<T>
Auto Trait Implementations§
impl<T: ?Sized> RefUnwindSafe for Json<T>where T: RefUnwindSafe,
impl<T: ?Sized> Send for Json<T>where T: Send,
impl<T: ?Sized> Sync for Json<T>where T: Sync,
impl<T: ?Sized> Unpin for Json<T>where T: Unpin,
impl<T: ?Sized> UnwindSafe for Json<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> CallHasher for Twhere
T: Hash + ?Sized,
impl<T> CallHasher for Twhere T: Hash + ?Sized,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.