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

Code before:
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::json_object;
    /// #     use diesel::sql_types::{Json};
    /// #     use serde_json::Value;
    /// #     let connection = &mut establish_connection();
    /// let json = diesel::select(json_object(vec!["hello","world"])).get_result::<Value>(connection)?;
    /// let expected:Value = serde_json::from_str(r#"{"hello":"world"}"#).unwrap();
    /// assert_eq!(expected,json);
    ///
    /// let json = diesel::select(json_object(vec!["hello","world","John","Doe"])).get_result::<Value>(connection)?;
    /// let expected:Value = serde_json::from_str(r#"{"hello":"world","John":"Doe"}"#).unwrap();
    /// assert_eq!(expected,json);
    ///
    /// let json = diesel::select(json_object(vec!["hello","world","John"])).get_result::<Value>(connection);
    /// assert!(json.is_err());
    ///
    /// #     Ok(())
    /// # }
    /// ```
    fn json_object(text_array:Array<Text>)->Json;
}


Review comment (suggestion):
Arrays in diesel are always generate arrays with nullable elements, with will make impossible to use this methods with array columns from tables. But this method breaks if we pass a array to them.
I would like @weiznich opinion here

Code after:
    ///
    // This function requires postgres >= 16.0
    // which we cannot expect to be widly used at the
    // point of writing this comment, so we skip running this test
    /// ```rust,no_run
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::array_shuffle;
    /// #     use diesel::sql_types::{Array, Integer};
    /// #     let connection = &mut establish_connection();
    /// let shuffled = diesel::select(array_shuffle::<Array<Integer>, _>(vec![1, 2, 3, 4, 5]))
    ///     .get_result::<Vec<i32>>(connection)?;
    /// assert_eq!(5, shuffled.len());
    /// assert_eq!(shuffled.iter().sum::<i32>(), 15);
    /// #     Ok(())
    /// # }
    /// ```
    fn array_shuffle<Arr: ArrayOrNullableArray + SingleValue>(array: Arr) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
    /// Converts any SQL value 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::to_json;
    /// #     use serde_json::{json, Value};
    /// #     use diesel::sql_types::{Integer, Array, Json, Text, Nullable};
    /// #     let connection = &mut establish_connection();
    /// let result = diesel::select(to_json::<Integer, _>(1))
    ///     .get_result::<Value>(connection)?;
    ///
    /// assert_eq!(json!(1), result);
    ///
    /// let result = diesel::select(to_json::<Array<Text>, _>(vec!["abc", "xyz"]))