mirror of
https://github.com/tokio-rs/axum.git
synced 2025-04-05 14:01:05 +02:00
Store Bytes in ErasedJson (#672)
This commit is contained in:
parent
d602682821
commit
031e0fd472
2 changed files with 8 additions and 4 deletions
axum-extra
|
@ -15,6 +15,7 @@ erased-json = ["serde", "serde_json"]
|
|||
|
||||
[dependencies]
|
||||
axum = { path = "../axum", version = "0.4" }
|
||||
bytes = "1.1.0"
|
||||
http = "0.2"
|
||||
mime = "0.3"
|
||||
pin-project-lite = "0.2"
|
||||
|
|
|
@ -3,6 +3,7 @@ use axum::{
|
|||
http::{header, HeaderValue, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use serde::Serialize;
|
||||
|
||||
/// A response type that holds a JSON in serialized form.
|
||||
|
@ -30,17 +31,19 @@ use serde::Serialize;
|
|||
/// ```
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "erased-json")))]
|
||||
#[derive(Debug)]
|
||||
pub struct ErasedJson(serde_json::Result<Vec<u8>>);
|
||||
pub struct ErasedJson(serde_json::Result<Bytes>);
|
||||
|
||||
impl ErasedJson {
|
||||
/// Create an `ErasedJson` by serializing a value with the compact formatter.
|
||||
pub fn new<T: Serialize>(val: T) -> Self {
|
||||
Self(serde_json::to_vec(&val))
|
||||
let mut bytes = BytesMut::with_capacity(128);
|
||||
Self(serde_json::to_writer((&mut bytes).writer(), &val).map(|_| bytes.freeze()))
|
||||
}
|
||||
|
||||
/// Create an `ErasedJson` by serializing a value with the pretty formatter.
|
||||
pub fn pretty<T: Serialize>(val: T) -> Self {
|
||||
Self(serde_json::to_vec_pretty(&val))
|
||||
let mut bytes = BytesMut::with_capacity(128);
|
||||
Self(serde_json::to_writer_pretty((&mut bytes).writer(), &val).map(|_| bytes.freeze()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +60,7 @@ impl IntoResponse for ErasedJson {
|
|||
}
|
||||
};
|
||||
|
||||
let mut res = Response::new(body::boxed(Full::from(bytes)));
|
||||
let mut res = Response::new(body::boxed(Full::new(bytes)));
|
||||
res.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()),
|
||||
|
|
Loading…
Add table
Reference in a new issue