From a3cf025d5418d5e87fb9b39065aecb2742e17efe Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 20 Jul 2022 18:32:08 +0200 Subject: [PATCH] Serialize Json to Bytes instead of Vec in IntoResponse (#1178) --- axum/src/json.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/axum/src/json.rs b/axum/src/json.rs index e1d068fa..d496f607 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -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, @@ -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) => (