Skip to main content

JsonResource

Trait JsonResource 

Source
pub trait JsonResource {
    type Model;

    // Required method
    fn to_json(model: &Self::Model) -> Json;

    // Provided methods
    fn wrap() -> Option<&'static str> { ... }
    fn make(model: &Self::Model) -> ResourceResponse { ... }
    fn collection<'a, I>(models: I) -> ResourceResponse
       where I: IntoIterator<Item = &'a Self::Model>,
             Self::Model: 'a { ... }
}
Expand description

How a model becomes JSON. Implement it once per type that leaves the server.

Required Associated Types§

Required Methods§

Source

fn to_json(model: &Self::Model) -> Json

The representation of one model.

Provided Methods§

Source

fn wrap() -> Option<&'static str>

The key the data sits under. Some("data") by default, as in Laravel; None sends the object or array bare.

Source

fn make(model: &Self::Model) -> ResourceResponse

Source

fn collection<'a, I>(models: I) -> ResourceResponse
where I: IntoIterator<Item = &'a Self::Model>, Self::Model: 'a,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§