Add support for returning pretty JSON response in `axum_extra::response::ErasedJson` (#662)

This commit is contained in:
Guy Lapid 2021-12-28 11:19:24 +02:00 committed by GitHub
parent 4fd7e927ba
commit d602682821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -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)

View File

@ -33,10 +33,15 @@ use serde::Serialize;
pub struct ErasedJson(serde_json::Result<Vec<u8>>);
impl ErasedJson {
/// Create an `ErasedJson` by serializing a value.
/// 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))
}
/// 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))
}
}
impl IntoResponse for ErasedJson {