pub struct RapidJson<T>(pub T);Expand description
JSON extractor and responder.
Json has two uses: JSON responses, and extracting typed data from JSON request payloads.
Extractor
To extract typed data from a request body, the inner type T must implement the
serde::Deserialize trait.
Use JsonConfig to configure extraction options.
use actix_web::{post, web, App};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
/// deserialize `Info` from request's body
#[post("/")]
async fn index(info: web::Json<Info>) -> String {
format!("Welcome {}!", info.username)
}Responder
The Json type JSON formatted responses. A handler may return a value of type
Json<T> where T is the type of a structure to serialize into JSON. The type T must
implement serde::Serialize.
use actix_web::{post, web, HttpRequest};
use serde::Serialize;
#[derive(Serialize)]
struct Info {
name: String,
}
#[post("/{name}")]
async fn index(req: HttpRequest) -> web::Json<Info> {
web::Json(Info {
name: req.match_info().get("name").unwrap().to_owned(),
})
}Tuple Fields§
§0: TImplementations§
source§impl<T> Json<T>
impl<T> Json<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap into inner T value.
Trait Implementations§
source§impl<T> FromRequest for Json<T>where
T: DeserializeOwned,
impl<T> FromRequest for Json<T>where T: DeserializeOwned,
See here for example of usage as an extractor.
source§fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Json<T> as FromRequest>::Future
fn from_request( req: &HttpRequest, payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>> ) -> <Json<T> as FromRequest>::Future
Self from request parts asynchronously.source§impl<T> Responder for Json<T>where
T: Serialize,
impl<T> Responder for Json<T>where T: Serialize,
Creates response with OK status code, correct content type header, and serialized JSON payload.
If serialization failed
type Body = EitherBody<String, BoxBody>
source§fn respond_to(
self,
_: &HttpRequest
) -> HttpResponse<<Json<T> as Responder>::Body>
fn respond_to( self, _: &HttpRequest ) -> HttpResponse<<Json<T> as Responder>::Body>
HttpResponse.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,
Auto Trait Implementations§
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> UnwindSafe for Json<T>where T: UnwindSafe,
Blanket Implementations§
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> 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<T, P> Resource for Twhere
T: DerefMut<Target = Path<P>>,
P: ResourcePath,
impl<T, P> Resource for Twhere T: DerefMut<Target = Path<P>>, P: ResourcePath,
fn resource_path(&mut self) -> &mut Path<<T as Resource>::Path>
§impl<T, I> Stylize<T> for Iwhere
I: Into<Styled<T>>,
impl<T, I> Stylize<T> for Iwhere I: Into<Styled<T>>,
fn black(self) -> Styled<T>
fn dark_grey(self) -> Styled<T>
fn light_red(self) -> Styled<T>
fn red(self) -> Styled<T>
fn light_green(self) -> Styled<T>
fn green(self) -> Styled<T>
fn light_yellow(self) -> Styled<T>
fn yellow(self) -> Styled<T>
fn light_blue(self) -> Styled<T>
fn blue(self) -> Styled<T>
fn light_magenta(self) -> Styled<T>
fn magenta(self) -> Styled<T>
fn light_cyan(self) -> Styled<T>
fn cyan(self) -> Styled<T>
fn white(self) -> Styled<T>
fn grey(self) -> Styled<T>
fn rgb(self, r: u8, g: u8, b: u8) -> Styled<T>
fn ansi(self, ansi: u8) -> Styled<T>
fn on_black(self) -> Styled<T>
fn on_dark_grey(self) -> Styled<T>
fn on_light_red(self) -> Styled<T>
fn on_red(self) -> Styled<T>
fn on_light_green(self) -> Styled<T>
fn on_green(self) -> Styled<T>
fn on_light_yellow(self) -> Styled<T>
fn on_yellow(self) -> Styled<T>
fn on_light_blue(self) -> Styled<T>
fn on_blue(self) -> Styled<T>
fn on_light_magenta(self) -> Styled<T>
fn on_magenta(self) -> Styled<T>
fn on_light_cyan(self) -> Styled<T>
fn on_cyan(self) -> Styled<T>
fn on_white(self) -> Styled<T>
fn on_grey(self) -> Styled<T>
fn on_rgb(self, r: u8, g: u8, b: u8) -> Styled<T>
fn on_ansi(self, ansi: u8) -> Styled<T>
fn bold(self) -> Styled<T>
fn underlined(self) -> Styled<T>
fn reverse(self) -> Styled<T>
fn dim(self) -> Styled<T>
fn italic(self) -> Styled<T>
fn slow_blink(self) -> Styled<T>
fn rapid_blink(self) -> Styled<T>
fn crossed_out(self) -> Styled<T>
§impl<T> Widget for Twhere
T: Deref<Target = str> + ?Sized,
impl<T> Widget for Twhere T: Deref<Target = str> + ?Sized,
§fn render<B>(
&mut self,
layout: &mut Layout,
backend: &mut B
) -> Result<(), Error>where
B: Backend,
fn render<B>( &mut self, layout: &mut Layout, backend: &mut B ) -> Result<(), Error>where B: Backend,
Does not allow multi-line strings. If the string requires more than a single line, it adds cuts it short and adds ‘…’ to the end.
If a multi-line string is required, use the Text widget.
§fn height(&mut self, layout: &mut Layout) -> u16
fn height(&mut self, layout: &mut Layout) -> u16
Does not allow multi-line strings.
If a multi-line string is required, use the Text widget.
§fn cursor_pos(&mut self, layout: Layout) -> (u16, u16)
fn cursor_pos(&mut self, layout: Layout) -> (u16, u16)
Returns the location of the first character
§fn handle_key(&mut self, _: KeyEvent) -> bool
fn handle_key(&mut self, _: KeyEvent) -> bool
This widget does not handle any events