Repository: diesel-rs/diesel
File: diesel/src/pg/expression/functions.rs (Rust)

Code before:
    ) -> Arr2::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
    /// This function row_to_json takes Record type as an Input o convert it to JSON.
    ///
    /// # Example
    ///
    /// ```rust
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     #[cfg(feature = "serde_json")]
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # #[cfg(feature = "serde_json")]
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::sql;
    /// #     use diesel::sql_types::{Record, Text, Integer};
    /// #     use serde_json::Value;
    /// #     let connection = &mut establish_connection();
    ///
    ///     // Example SQL query selecting a custom record type
    ///     let query = diesel::select(sql::<Record<(Text, Integer)>>("ROW('John', 30)::record"));
    ///
    ///     // Running the query and fetching the result as JSON
    ///     let (name, age): (String, i32) = query.get_result(connection)?;
    ///
    ///     // Create a Json Value
    ///     let json_value: Value = serde_json::json!({
    ///         "name": name,
    ///         "age": age
    ///     });
    ///
    ///     // Expected JSON output from the Record type
    ///     let expected: Value = serde_json::json!({
    ///         "name": "John",
    ///         "age": 30
    ///     });
    ///
    ///     // Asserting the result
    ///     assert_eq!(expected, json_value);
    ///
    ///     Ok(())
    /// # }
    /// ```
    #[sql_name = "row_to_json"]
    fn row_to_json<R: RecordOrNullableRecord + SingleValue + MaybeNullableValue<Json>>(record: R) -> R::Out;
}

Review comment (suggestion):
Imho, `row_to_json` should be used here instead of `sql` so, the returned value could be casted to json right away.

Code after:
    ) -> Arr2::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
    /// This function `row_to_json` takes a Record type as an input and converts it to JSON.
    ///
    /// # Example
    ///
    /// ```rust
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     #[cfg(feature = "serde_json")]
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # #[cfg(feature = "serde_json")]
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::row_to_json;
    /// #     use diesel::dsl::sql;
    /// #     use diesel::sql_types::{Record, Text, Integer};
    /// #     use serde_json::Value;
    /// #     let connection = &mut establish_connection();
    ///
    /// let json_value = diesel::select(row_to_json(sql::<Record<(Text, Integer)>>(
    ///     "ROW('John', 30)"
    /// )))
    /// .get_result::<Value>(connection)?;
    /// let expected: Value = serde_json::json!({
    ///     "f1": "John",
    ///     "f2": 30
    /// });
    /// assert_eq!(expected, json_value);
    ///
    /// let json_value = diesel::select(row_to_json(sql::<Record<()>>("ROW()")))
    /// .get_result::<Value>(connection)?;
    /// let expected: Value = serde_json::json!({});
    /// assert_eq!(expected, json_value);
    ///
    /// #    Ok(())
    /// # }
    /// ```
    #[sql_name = "row_to_json"]
    fn row_to_json<R: RecordOrNullableRecord + MaybeNullableValue<Json>>(record: R) -> R::Out;
}