torrust_tracker/console/clients/udp/responses/
json.rs

1use anyhow::Context;
2use serde::Serialize;
3
4use super::dto::SerializableResponse;
5
6#[allow(clippy::module_name_repetitions)]
7pub trait ToJson {
8    ///
9    /// Returns a string with the JSON serialized version of the response
10    ///
11    /// # Errors
12    ///
13    /// Will return an error if serialization fails.
14    ///
15    fn to_json_string(&self) -> anyhow::Result<String>
16    where
17        Self: Serialize,
18    {
19        let pretty_json = serde_json::to_string_pretty(self).context("response JSON serialization")?;
20
21        Ok(pretty_json)
22    }
23}
24
25impl ToJson for SerializableResponse {}