Serialize Json<T> to Bytes instead of Vec<u8> in IntoResponse (#1178)

This commit is contained in:
Jonas Platte 2022-07-20 18:32:08 +02:00 committed by GitHub
parent a3eaa332e4
commit a3cf025d54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@ use crate::{
}; };
use async_trait::async_trait; use async_trait::async_trait;
use axum_core::response::{IntoResponse, Response}; use axum_core::response::{IntoResponse, Response};
use bytes::{BufMut, BytesMut};
use http::{ use http::{
header::{self, HeaderValue}, header::{self, HeaderValue},
StatusCode, StatusCode,
@ -185,13 +186,14 @@ where
T: Serialize, T: Serialize,
{ {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match serde_json::to_vec(&self.0) { let mut buf = BytesMut::new().writer();
Ok(bytes) => ( match serde_json::to_writer(&mut buf, &self.0) {
Ok(()) => (
[( [(
header::CONTENT_TYPE, header::CONTENT_TYPE,
HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()), HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()),
)], )],
bytes, buf.into_inner().freeze(),
) )
.into_response(), .into_response(),
Err(err) => ( Err(err) => (