Skip to content

Commit

Permalink
Serialize Json<T> to Bytes instead of Vec<u8> in IntoResponse (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored and davidpdrsn committed Jul 25, 2022
1 parent 9b620e6 commit ab1ccf8
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions axum/src/json.rs
Expand Up @@ -5,6 +5,7 @@ use crate::{
};
use async_trait::async_trait;
use axum_core::response::{IntoResponse, Response};
use bytes::{BufMut, BytesMut};
use http::{
header::{self, HeaderValue},
StatusCode,
Expand Down Expand Up @@ -185,13 +186,14 @@ where
T: Serialize,
{
fn into_response(self) -> Response {
match serde_json::to_vec(&self.0) {
Ok(bytes) => (
let mut buf = BytesMut::new().writer();
match serde_json::to_writer(&mut buf, &self.0) {
Ok(()) => (
[(
header::CONTENT_TYPE,
HeaderValue::from_static(mime::APPLICATION_JSON.as_ref()),
)],
bytes,
buf.into_inner().freeze(),
)
.into_response(),
Err(err) => (
Expand Down

0 comments on commit ab1ccf8

Please sign in to comment.