1
1
use async_std:: io:: prelude:: * ;
2
- use async_std:: io:: { self , BufRead , Read } ;
2
+ use async_std:: io:: { self , Cursor } ;
3
+ use serde:: { de:: DeserializeOwned , Serialize } ;
3
4
4
5
use std:: fmt:: { self , Debug } ;
5
6
use std:: pin:: Pin ;
@@ -157,6 +158,30 @@ impl Body {
157
158
Ok ( buf)
158
159
}
159
160
161
+ /// Creates a `Body` from a type, serializing it as JSON.
162
+ ///
163
+ /// # Mime
164
+ ///
165
+ /// The encoding is set to `application/json`.
166
+ ///
167
+ /// # Examples
168
+ ///
169
+ /// ```
170
+ /// use http_types::{Body, serde::json};
171
+ ///
172
+ /// let body = Body::from_json(json!({ "name": "Chashu" }));
173
+ /// # drop(body);
174
+ /// ```
175
+ pub fn from_json ( json : impl Serialize ) -> crate :: Result < Self > {
176
+ let bytes = serde_json:: to_vec ( & json) ?;
177
+ let body = Self {
178
+ length : Some ( bytes. len ( ) ) ,
179
+ reader : Box :: new ( Cursor :: new ( bytes) ) ,
180
+ mime : mime:: JSON ,
181
+ } ;
182
+ Ok ( body)
183
+ }
184
+
160
185
/// Get the length of the body in bytes.
161
186
///
162
187
/// # Examples
@@ -218,6 +243,31 @@ impl Body {
218
243
Ok ( result)
219
244
}
220
245
246
+ /// Parse the body as JSON, serializing it to a struct.
247
+ ///
248
+ /// # Examples
249
+ ///
250
+ /// ```
251
+ /// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async {
252
+ /// use http_types::Body;
253
+ /// use http_types::serde::{Serialize, Deserialize};
254
+ ///
255
+ /// #[derive(Debug, Serialize, Deserialize)]
256
+ /// struct Cat { name: String }
257
+ ///
258
+ /// let cat = Cat { name: String::from("chashu") };
259
+ /// let body = Body::from_json(cat)?;
260
+ ///
261
+ /// let cat: Cat = body.into_json().await?;
262
+ /// assert_eq!(&cat.name, "chashu");
263
+ /// # Ok(()) }) }
264
+ /// ```
265
+ pub async fn into_json < T : DeserializeOwned > ( mut self ) -> crate :: Result < T > {
266
+ let mut buf = Vec :: with_capacity ( 1024 ) ;
267
+ self . read_to_end ( & mut buf) . await ?;
268
+ Ok ( serde_json:: from_slice ( & buf) . map_err ( io:: Error :: from) ?)
269
+ }
270
+
221
271
pub ( crate ) fn mime ( & self ) -> & Mime {
222
272
& self . mime
223
273
}
@@ -236,7 +286,7 @@ impl From<String> for Body {
236
286
fn from ( s : String ) -> Self {
237
287
Self {
238
288
length : Some ( s. len ( ) ) ,
239
- reader : Box :: new ( io :: Cursor :: new ( s. into_bytes ( ) ) ) ,
289
+ reader : Box :: new ( Cursor :: new ( s. into_bytes ( ) ) ) ,
240
290
mime : mime:: PLAIN ,
241
291
}
242
292
}
@@ -246,7 +296,7 @@ impl<'a> From<&'a str> for Body {
246
296
fn from ( s : & ' a str ) -> Self {
247
297
Self {
248
298
length : Some ( s. len ( ) ) ,
249
- reader : Box :: new ( io :: Cursor :: new ( s. to_owned ( ) . into_bytes ( ) ) ) ,
299
+ reader : Box :: new ( Cursor :: new ( s. to_owned ( ) . into_bytes ( ) ) ) ,
250
300
mime : mime:: PLAIN ,
251
301
}
252
302
}
@@ -256,7 +306,7 @@ impl From<Vec<u8>> for Body {
256
306
fn from ( b : Vec < u8 > ) -> Self {
257
307
Self {
258
308
length : Some ( b. len ( ) ) ,
259
- reader : Box :: new ( io :: Cursor :: new ( b) ) ,
309
+ reader : Box :: new ( Cursor :: new ( b) ) ,
260
310
mime : mime:: BYTE_STREAM ,
261
311
}
262
312
}
0 commit comments