Struct sqlx::types::Json

source ยท
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<T> AsMut<T> for Json<T>

sourceยง

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
sourceยง

impl<T> AsRef<T> for Json<T>

sourceยง

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
sourceยง

impl<T> Clone for Json<T>
where T: Clone + ?Sized,

sourceยง

fn clone(&self) -> Json<T>

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
sourceยง

impl<T> Debug for Json<T>
where T: Debug + ?Sized,

sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
sourceยง

impl<'r, T> Decode<'r, MySql> for Json<T>
where T: 'r + Deserialize<'r>,

sourceยง

fn decode( value: MySqlValueRef<'r>, ) -> Result<Json<T>, Box<dyn Error + Sync + Send>>

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: 'r + Deserialize<'r>,

sourceยง

fn decode( value: PgValueRef<'r>, ) -> Result<Json<T>, Box<dyn Error + Sync + Send>>

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>,

sourceยง

fn decode( value: SqliteValueRef<'r>, ) -> Result<Json<T>, Box<dyn Error + Sync + Send>>

Decode a new value of this type using a raw value from the database.
sourceยง

impl<T> Default for Json<T>
where T: Default + ?Sized,

sourceยง

fn default() -> Json<T>

Returns the โ€œdefault valueโ€ for a type. Read more
sourceยง

impl<T> Deref for Json<T>

sourceยง

type Target = T

The resulting type after dereferencing.
sourceยง

fn deref(&self) -> &<Json<T> as Deref>::Target

Dereferences the value.
sourceยง

impl<T> DerefMut for Json<T>

sourceยง

fn deref_mut(&mut self) -> &mut <Json<T> as Deref>::Target

Mutably dereferences the value.
sourceยง

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>,

Deserialize this value from the given Serde deserializer. Read more
sourceยง

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>>

Writes the value of self into buf without moving self. Read more
sourceยง

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.
sourceยง

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

sourceยง

fn size_hint(&self) -> usize

sourceยง

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>>

Writes the value of self into buf without moving self. Read more
sourceยง

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.
sourceยง

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

sourceยง

fn size_hint(&self) -> usize

sourceยง

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>>

Writes the value of self into buf without moving self. Read more
sourceยง

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.
sourceยง

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

sourceยง

fn size_hint(&self) -> usize

sourceยง

impl<T> From<T> for Json<T>

sourceยง

fn from(value: T) -> Json<T>

Converts to this type from the input type.
sourceยง

impl<T> Hash for Json<T>
where T: Hash + ?Sized,

sourceยง

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
sourceยง

impl<T> Ord for Json<T>
where T: Ord + ?Sized,

sourceยง

fn cmp(&self, other: &Json<T>) -> Ordering

This method returns an Ordering between self and other. Read more
sourceยง

impl<T> PartialEq for Json<T>
where T: PartialEq + ?Sized,

sourceยง

fn eq(&self, other: &Json<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
sourceยง

impl<T> PartialOrd for Json<T>
where T: PartialOrd + ?Sized,

sourceยง

fn partial_cmp(&self, other: &Json<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 ยท sourceยง

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 ยท sourceยง

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 ยท sourceยง

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 ยท sourceยง

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
sourceยง

impl<T> PgHasArrayType for Json<T>

sourceยง

impl<T> Serialize for Json<T>
where T: Serialize + ?Sized,

sourceยง

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>

sourceยง

fn type_info() -> MySqlTypeInfo

Returns the canonical SQL type for this Rust type. Read more
sourceยง

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>

sourceยง

fn type_info() -> PgTypeInfo

Returns the canonical SQL type for this Rust type. Read more
sourceยง

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>

sourceยง

fn type_info() -> SqliteTypeInfo

Returns the canonical SQL type for this Rust type. Read more
sourceยง

fn compatible(ty: &SqliteTypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. Read more
sourceยง

impl<T> Copy for Json<T>
where T: Copy + ?Sized,

sourceยง

impl<T> Eq for Json<T>
where T: Eq + ?Sized,

sourceยง

impl<T> StructuralPartialEq for Json<T>
where T: ?Sized,

Auto Trait Implementationsยง

ยง

impl<T> Freeze for Json<T>
where T: Freeze + ?Sized,

ยง

impl<T> RefUnwindSafe for Json<T>
where T: RefUnwindSafe + ?Sized,

ยง

impl<T> Send for Json<T>
where T: Send + ?Sized,

ยง

impl<T> Sync for Json<T>
where T: Sync + ?Sized,

ยง

impl<T> Unpin for Json<T>
where T: Unpin + ?Sized,

ยง

impl<T> UnwindSafe for Json<T>
where T: UnwindSafe + ?Sized,

Blanket Implementationsยง

sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> CloneToUninit for T
where T: Clone,

sourceยง

unsafe fn clone_to_uninit(&self, dst: *mut T)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
sourceยง

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

sourceยง

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

sourceยง

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
sourceยง

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

sourceยง

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
sourceยง

impl<T> From<!> for T

sourceยง

fn from(t: !) -> T

Converts to this type from the input type.
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

sourceยง

impl<T> Instrument for T

sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

sourceยง

impl<T> IntoEither for T

sourceยง

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 more
sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ“˜
where F: FnOnce(&Self) -> bool,

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
sourceยง

impl<T> Same for T

sourceยง

type Output = T

Should always be Self
sourceยง

impl<T> ToOwned for T
where T: Clone,

sourceยง

type Owned = T

The resulting type after obtaining ownership.
sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
sourceยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

sourceยง

fn vzip(self) -> V

sourceยง

impl<T> WithSubscriber for T

sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
sourceยง

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

sourceยง

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

sourceยง

impl<T> Parsable for T
where T: Deref, <T as Deref>::Target: Parsable,