From d602682821ded71eac446f208c2dec569af1b0f0 Mon Sep 17 00:00:00 2001 From: Guy Lapid <53928652+guylapid@users.noreply.github.com> Date: Tue, 28 Dec 2021 11:19:24 +0200 Subject: [PATCH] Add support for returning pretty JSON response in `axum_extra::response::ErasedJson` (#662) --- axum-extra/CHANGELOG.md | 2 ++ axum-extra/src/response/erased_json.rs | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 6db113c1..080e4526 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -14,8 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # 0.1.1 (27. December, 2021) - Add `middleware::from_fn` for creating middleware from async functions ([#656]) +- Add support for returning pretty JSON response in `response::ErasedJson` ([#662]) [#656]: https://github.com/tokio-rs/axum/pull/656 +[#662]: https://github.com/tokio-rs/axum/pull/662 # 0.1.0 (02. December, 2021) diff --git a/axum-extra/src/response/erased_json.rs b/axum-extra/src/response/erased_json.rs index cf1b675f..6b50da7c 100644 --- a/axum-extra/src/response/erased_json.rs +++ b/axum-extra/src/response/erased_json.rs @@ -33,10 +33,15 @@ use serde::Serialize; pub struct ErasedJson(serde_json::Result>); impl ErasedJson { - /// Create an `ErasedJson` by serializing a value. + /// Create an `ErasedJson` by serializing a value with the compact formatter. pub fn new(val: T) -> Self { Self(serde_json::to_vec(&val)) } + + /// Create an `ErasedJson` by serializing a value with the pretty formatter. + pub fn pretty(val: T) -> Self { + Self(serde_json::to_vec_pretty(&val)) + } } impl IntoResponse for ErasedJson {