pub struct Json<T>(pub T);Expand description
A field wrapper that stores T as a JSON-encoded string in the database.
Use Json<T> as a model field type when the column has no native database
representation for T — for example, a serde-derived struct, a HashMap,
or any other type that round-trips through
serde::Serialize and
serde::Deserialize. The value is JSON-encoded on
insert and update, and decoded on read.
The column is stored as TEXT (or the backend equivalent). Use a
#[column(type = "jsonb")] annotation (or similar) on the field if a
driver-native JSON column type is preferred.
§Two nullable variants
Json<T> composes with Option in two distinct, both-useful ways:
| Field type | None is stored as |
|---|---|
Option<Json<T>> | SQL NULL |
Json<Option<T>> | JSON literal "null" |
§Setter ergonomics
The crate provides an IntoExpr<Json<T>> impl for any
T: serde::Serialize, so create / update setters and the create!
macro accept the bare inner value without an explicit Json(...)
wrapper. Both forms produce the same expression:
// for a `payload: Json<Payload>` (or `Deferred<Json<Payload>>`) field
Repository::create().payload(my_payload.clone()).exec(&mut db).await?;
Repository::create().payload(Json(my_payload.clone())).exec(&mut db).await?;The Json(...) form is still useful when type inference needs a
nudge (e.g., comparison expressions like .eq(Json("hello"))).
§Composition with Deferred
Json<T> is the only wrapper allowed inside Deferred
for a serde-typed field, since a deferred column needs both lazy
loading and a serializer the macro can drive through trait dispatch:
#[derive(Debug, toasty::Model)]
struct Repository {
#[key] #[auto]
id: uuid::Uuid,
schema: toasty::Deferred<toasty::Json<MySchema>>,
}§Examples
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Tags(Vec<String>);
#[derive(Debug, toasty::Model)]
struct Item {
#[key] #[auto]
id: i64,
tags: toasty::Json<Tags>,
}Tuple Fields§
§0: TTrait Implementations§
Source§impl<T> Assign<Json<T>> for Json<T>where
T: Serialize,
impl<T> Assign<Json<T>> for Json<T>where
T: Serialize,
Source§fn into_assignment(self) -> Assignment<Json<T>>
fn into_assignment(self) -> Assignment<Json<T>>
Assignment<T> value.Source§fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
Assignments] map at
the specified projection.impl<T> Copy for Json<T>where
T: Copy,
Source§impl<'de, T> Deserialize<'de> for Json<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Json<T>where
T: Deserialize<'de>,
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>,
impl<T> Eq for Json<T>where
T: Eq,
Source§impl<T> Field for Json<T>where
T: Serialize + for<'de> Deserialize<'de>,
impl<T> Field for Json<T>where
T: Serialize + for<'de> Deserialize<'de>,
Source§fn field_ty(storage_ty: Option<Type>) -> FieldTy
fn field_ty(storage_ty: Option<Type>) -> FieldTy
Tag the schema entry as a JSON-serialized String column.
The macro never inspects the wrapper at the AST level; the
serialize: Some(Json) metadata flows through trait dispatch
alongside the storage type, so external schema consumers can still
see that the column is JSON-typed even though the encoding itself is
invisible to the macro.
Source§type ExprTarget = Json<T>
type ExprTarget = Json<T>
Source§type Path<Origin> = Path<Origin, Json<T>>
type Path<Origin> = Path<Origin, Json<T>>
Source§type ListPath<Origin> = Path<Origin, List<<Json<T> as Field>::ExprTarget>>
type ListPath<Origin> = Path<Origin, List<<Json<T> as Field>::ExprTarget>>
Source§type Update<'a> = ()
type Update<'a> = ()
Source§type Inner = Json<T>
type Inner = Json<T>
Option<T>, Box<T>,
Arc<T>, and Rc<T> map to T::Inner.Source§fn new_path<Origin>(
path: Path<Origin, Json<T>>,
) -> <Json<T> as Field>::Path<Origin>
fn new_path<Origin>( path: Path<Origin, Json<T>>, ) -> <Json<T> as Field>::Path<Origin>
Self::ExprTarget. Read moreSource§fn new_list_path<Origin>(
path: Path<Origin, List<<Json<T> as Field>::ExprTarget>>,
) -> <Json<T> as Field>::ListPath<Origin>
fn new_list_path<Origin>( path: Path<Origin, List<<Json<T> as Field>::ExprTarget>>, ) -> <Json<T> as Field>::ListPath<Origin>
Source§fn new_update<'a>(
_assignments: &'a mut Assignments,
_projection: Projection,
) -> <Json<T> as Field>::Update<'a>
fn new_update<'a>( _assignments: &'a mut Assignments, _projection: Projection, ) -> <Json<T> as Field>::Update<'a>
() (no builder).
For embedded types, this is overridden to construct the {Type}Update builder.Source§impl<T> Load for Json<T>where
T: for<'de> Deserialize<'de>,
impl<T> Load for Json<T>where
T: for<'de> Deserialize<'de>,
Source§fn ty() -> Type
fn ty() -> Type
stmt::Type that describes values of this type.Source§fn reload(target: &mut Json<T>, value: Value) -> Result<(), Error>
fn reload(target: &mut Json<T>, value: Value) -> Result<(), Error>
Source§fn ty_relation() -> Type
fn ty_relation() -> Type
stmt::Type used when this type appears as a relation
target. The default delegates to ty().Source§impl<T> Serialize for Json<T>where
T: Serialize,
impl<T> Serialize for Json<T>where
T: Serialize,
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,
impl<T> StructuralPartialEq for Json<T>where
T: PartialEq,
Auto Trait Implementations§
impl<T> Freeze for Json<T>where
T: Freeze,
impl<T> RefUnwindSafe for Json<T>where
T: RefUnwindSafe,
impl<T> Send for Json<T>where
T: Send,
impl<T> Sync for Json<T>where
T: Sync,
impl<T> Unpin for Json<T>where
T: Unpin,
impl<T> UnsafeUnpin for Json<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Json<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> Assign<Arc<T>> for Twhere
T: IntoExpr<T>,
impl<T> Assign<Arc<T>> for Twhere
T: IntoExpr<T>,
Source§fn into_assignment(self) -> Assignment<Arc<T>>
fn into_assignment(self) -> Assignment<Arc<T>>
Assignment<T> value.Source§fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
Assignments] map at
the specified projection.Source§impl<T> Assign<Box<T>> for Twhere
T: IntoExpr<T>,
impl<T> Assign<Box<T>> for Twhere
T: IntoExpr<T>,
Source§fn into_assignment(self) -> Assignment<Box<T>>
fn into_assignment(self) -> Assignment<Box<T>>
Assignment<T> value.Source§fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
Assignments] map at
the specified projection.Source§impl<T> Assign<Json<T>> for Twhere
T: Serialize,
impl<T> Assign<Json<T>> for Twhere
T: Serialize,
Source§fn into_assignment(self) -> Assignment<Json<T>>
fn into_assignment(self) -> Assignment<Json<T>>
Assignment<T> value.Source§fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
Assignments] map at
the specified projection.Source§impl<T> Assign<Option<T>> for Twhere
T: IntoExpr<T>,
impl<T> Assign<Option<T>> for Twhere
T: IntoExpr<T>,
Source§fn into_assignment(self) -> Assignment<Option<T>>
fn into_assignment(self) -> Assignment<Option<T>>
Assignment<T> value.Source§fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
Assignments] map at
the specified projection.Source§impl<T> Assign<Rc<T>> for Twhere
T: IntoExpr<T>,
impl<T> Assign<Rc<T>> for Twhere
T: IntoExpr<T>,
Source§fn into_assignment(self) -> Assignment<Rc<T>>
fn into_assignment(self) -> Assignment<Rc<T>>
Assignment<T> value.Source§fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
fn assign(self, assignments: &mut Assignments, projection: Projection)where
Self: Sized,
Assignments] map at
the specified projection.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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<R> CryptoRng for R
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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>
impl<R> RngCore for Rwhere
R: Rng,
Source§impl<R> RngExt for R
impl<R> RngExt for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read more