Skip to main content

Json

Struct Json 

Source
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 typeNone 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: 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> Assign<Json<T>> for Json<T>
where T: Serialize,

Source§

fn into_assignment(self) -> Assignment<Json<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for Json<T>
where T: Copy,

Source§

impl<T> Debug for Json<T>
where T: Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Json<T>
where T: Default,

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) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for Json<T>

Source§

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

Mutably dereferences the value.
Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Display for Json<T>
where T: Display,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Eq for Json<T>
where T: Eq,

Source§

impl<T> Field for Json<T>
where T: Serialize + for<'de> Deserialize<'de>,

Source§

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>

The expression-level type of this field. Read more
Source§

type Path<Origin> = Path<Origin, Json<T>>

The type returned when accessing this field from a Fields struct. For primitives, this is Path<Origin, Self>. For embedded types, this is {Type}Fields.
Source§

type ListPath<Origin> = Path<Origin, List<<Json<T> as Field>::ExprTarget>>

The type returned when accessing this field from a list Fields struct. For primitives, this is Path<Origin, ListSelf::ExprTarget>. For embedded types, this is {Type}ListFields.
Source§

type Update<'a> = ()

The type of the update builder for this field. For embedded types, this is {Type}Update<’a>. For primitives, this will be {Type}Update<’a> once implemented.
Source§

type Inner = Json<T>

The unwrapped type used for foreign key filter expressions. Primitives map to themselves. Wrappers like 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>

Build a field path from a raw path of the field’s Self::ExprTarget. Read more
Source§

fn new_list_path<Origin>( path: Path<Origin, List<<Json<T> as Field>::ExprTarget>>, ) -> <Json<T> as Field>::ListPath<Origin>

Build a list field path from a raw path. For primitives, returns the path as-is. For embedded types, wraps the path in a ListFields struct.
Source§

fn new_update<'a>( _assignments: &'a mut Assignments, _projection: Projection, ) -> <Json<T> as Field>::Update<'a>

Build an update builder from assignments and a projection. For primitives, this returns () (no builder). For embedded types, this is overridden to construct the {Type}Update builder.
Source§

fn key_constraint<Origin>( &self, _target: Path<Origin, <Json<T> as Field>::Inner>, ) -> Expr<bool>

Build a boolean filter expression comparing this field value against a target path. Used by generated belongs-to accessors to build FK lookups. Read more
Source§

const NULLABLE: bool = false

Whether or not the type is nullable
Source§

const DEFERRED: bool = false

Whether this field is omitted from default loads.
Source§

fn register(_model_set: &mut ModelSet)

Register any models referenced by this field type into the given ModelSet. Read more
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,

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> IntoExpr<Json<T>> for Json<T>
where T: Serialize,

Source§

fn into_expr(self) -> Expr<Json<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Json<T>>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> Load for Json<T>
where T: for<'de> Deserialize<'de>,

Source§

type Output = Json<T>

The concrete type produced by load. Read more
Source§

fn ty() -> Type

Returns the stmt::Type that describes values of this type.
Source§

fn load(value: Value) -> Result<Json<T>, Error>

Deserialize a database Value into Self::Output. Read more
Source§

fn reload(target: &mut Json<T>, value: Value) -> Result<(), Error>

Reload the value in-place from a value returned by the database. Read more
Source§

fn ty_relation() -> Type

Returns the stmt::Type used when this type appears as a relation target. The default delegates to ty().
Source§

fn load_relation(value: Value) -> Result<Self::Output, Error>

Deserialize a value that was loaded as a relation target. Read more
Source§

impl<T> PartialEq for Json<T>
where T: PartialEq,

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

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,

Serialize this value into the given Serde serializer. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Assign<Arc<T>> for T
where T: IntoExpr<T>,

Source§

fn into_assignment(self) -> Assignment<Arc<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
Source§

impl<T> Assign<Box<T>> for T
where T: IntoExpr<T>,

Source§

fn into_assignment(self) -> Assignment<Box<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
Source§

impl<T> Assign<Json<T>> for T
where T: Serialize,

Source§

fn into_assignment(self) -> Assignment<Json<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
Source§

impl<T> Assign<Option<T>> for T
where T: IntoExpr<T>,

Source§

fn into_assignment(self) -> Assignment<Option<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
Source§

impl<T> Assign<Rc<T>> for T
where T: IntoExpr<T>,

Source§

fn into_assignment(self) -> Assignment<Rc<T>>

Convert into an Assignment<T> value.
Source§

fn assign(self, assignments: &mut Assignments, projection: Projection)
where Self: Sized,

Record one or more assignments into the given [Assignments] map at the specified projection.
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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

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

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<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<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> IntoExpr<Arc<T>> for T
where T: IntoExpr<T>,

Source§

fn into_expr(self) -> Expr<Arc<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Arc<T>>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> IntoExpr<Box<T>> for T
where T: IntoExpr<T>,

Source§

fn into_expr(self) -> Expr<Box<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Box<T>>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> IntoExpr<Json<T>> for T
where T: Serialize,

Source§

fn into_expr(self) -> Expr<Json<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Json<T>>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> IntoExpr<Option<T>> for T
where T: IntoExpr<T>,

Source§

fn into_expr(self) -> Expr<Option<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Option<T>>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<T> IntoExpr<Rc<T>> for T
where T: IntoExpr<T>,

Source§

fn into_expr(self) -> Expr<Rc<T>>

Consume self and produce an Expr<T>. Read more
Source§

fn by_ref(&self) -> Expr<Rc<T>>

Produce an Expr<T> from a reference without consuming self. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<R> RngCore for R
where R: Rng,

Source§

impl<R> RngExt for R
where R: Rng + ?Sized,

Source§

fn random<T>(&mut self) -> T

Return a random value via the StandardUniform distribution. Read more
Source§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>

Return an iterator over random variates Read more
Source§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn random_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut [T])
where T: Fill,

Fill any type implementing Fill with random data 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<R> TryCryptoRng for R
where R: DerefMut, <R as Deref>::Target: TryCryptoRng,

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<R> TryRng for R
where R: DerefMut, <R as Deref>::Target: TryRng,

Source§

type Error = <<R as Deref>::Target as TryRng>::Error

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, <R as TryRng>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRng>::Error>

Return the next random u64.
Source§

fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), <R as TryRng>::Error>

Fill dst entirely with random data.
Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0:

use TryRng instead

Error type.
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