mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
remove Json type
This commit is contained in:
parent
ee62814216
commit
c368abdf5f
101
src/json.rs
101
src/json.rs
@ -1,8 +1,6 @@
|
|||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use futures::{Future, Poll, Stream};
|
use futures::{Future, Poll, Stream};
|
||||||
use http::header::CONTENT_LENGTH;
|
use http::header::CONTENT_LENGTH;
|
||||||
use std::fmt;
|
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
|
|
||||||
use mime;
|
use mime;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
@ -11,105 +9,6 @@ use serde_json;
|
|||||||
use error::JsonPayloadError;
|
use error::JsonPayloadError;
|
||||||
use httpmessage::HttpMessage;
|
use httpmessage::HttpMessage;
|
||||||
|
|
||||||
/// Json helper
|
|
||||||
///
|
|
||||||
/// Json can be used for two different purpose. First is for json response
|
|
||||||
/// generation and second is for extracting typed information from request's
|
|
||||||
/// payload.
|
|
||||||
///
|
|
||||||
/// To extract typed information from request's body, the type `T` must
|
|
||||||
/// implement the `Deserialize` trait from *serde*.
|
|
||||||
///
|
|
||||||
/// [**JsonConfig**](dev/struct.JsonConfig.html) allows to configure extraction
|
|
||||||
/// process.
|
|
||||||
///
|
|
||||||
/// ## Example
|
|
||||||
///
|
|
||||||
/// ```rust,ignore
|
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// #[macro_use] extern crate serde_derive;
|
|
||||||
/// use actix_web::{App, Json, Result, http};
|
|
||||||
///
|
|
||||||
/// #[derive(Deserialize)]
|
|
||||||
/// struct Info {
|
|
||||||
/// username: String,
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// /// deserialize `Info` from request's body
|
|
||||||
/// fn index(info: Json<Info>) -> Result<String> {
|
|
||||||
/// Ok(format!("Welcome {}!", info.username))
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn main() {
|
|
||||||
/// let app = App::new().resource(
|
|
||||||
/// "/index.html",
|
|
||||||
/// |r| r.method(http::Method::POST).with(index)); // <- use `with` extractor
|
|
||||||
/// }
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// The `Json` type allows you to respond with well-formed JSON data: simply
|
|
||||||
/// return a value of type Json<T> where T is the type of a structure
|
|
||||||
/// to serialize into *JSON*. The type `T` must implement the `Serialize`
|
|
||||||
/// trait from *serde*.
|
|
||||||
///
|
|
||||||
/// ```rust,ignore
|
|
||||||
/// # extern crate actix_web;
|
|
||||||
/// # #[macro_use] extern crate serde_derive;
|
|
||||||
/// # use actix_web::*;
|
|
||||||
/// #
|
|
||||||
/// #[derive(Serialize)]
|
|
||||||
/// struct MyObj {
|
|
||||||
/// name: String,
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// fn index(req: HttpRequest) -> Result<Json<MyObj>> {
|
|
||||||
/// Ok(Json(MyObj {
|
|
||||||
/// name: req.match_info().query("name")?,
|
|
||||||
/// }))
|
|
||||||
/// }
|
|
||||||
/// # fn main() {}
|
|
||||||
/// ```
|
|
||||||
pub struct Json<T>(pub T);
|
|
||||||
|
|
||||||
impl<T> Json<T> {
|
|
||||||
/// Deconstruct to an inner value
|
|
||||||
pub fn into_inner(self) -> T {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Deref for Json<T> {
|
|
||||||
type Target = T;
|
|
||||||
|
|
||||||
fn deref(&self) -> &T {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> DerefMut for Json<T> {
|
|
||||||
fn deref_mut(&mut self) -> &mut T {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Debug for Json<T>
|
|
||||||
where
|
|
||||||
T: fmt::Debug,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "Json: {:?}", self.0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> fmt::Display for Json<T>
|
|
||||||
where
|
|
||||||
T: fmt::Display,
|
|
||||||
{
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
fmt::Display::fmt(&self.0, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Request payload json parser that resolves to a deserialized `T` value.
|
/// Request payload json parser that resolves to a deserialized `T` value.
|
||||||
///
|
///
|
||||||
/// Returns error:
|
/// Returns error:
|
||||||
|
@ -144,7 +144,6 @@ pub use body::{Binary, Body};
|
|||||||
pub use error::{Error, ResponseError, Result};
|
pub use error::{Error, ResponseError, Result};
|
||||||
pub use extensions::Extensions;
|
pub use extensions::Extensions;
|
||||||
pub use httpmessage::HttpMessage;
|
pub use httpmessage::HttpMessage;
|
||||||
pub use json::Json;
|
|
||||||
pub use request::Request;
|
pub use request::Request;
|
||||||
pub use response::Response;
|
pub use response::Response;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user