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

Code before:
    /// # fn main() {
    /// #     run_test().unwrap();
    /// # }
    /// #
    /// # fn run_test() -> QueryResult<()> {
    /// #     use diesel::dsl::jsonb_object;
    /// #     use diesel::sql_types::{Array, Json, Nullable, Text};
    /// #     use serde_json::Value;
    /// #     let connection = &mut establish_connection();
    /// let jsonb = diesel::select(jsonb_object::<Array<Text>,_>(vec!["hello","world"]))
    ///                 .get_result::<Value>(connection)?;
    /// let expected:Value = serde_json::json!({"hello":"world"});
    /// assert_eq!(expected, jsonb);
    ///
    /// let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John","Doe"]))
    ///                 .get_result::<Value>(connection)?;
    /// let expected:Value = serde_json::json!({"hello": "world","John": "Doe"});
    /// assert_eq!(expected, jsonb);
    ///
    /// let jsonb = diesel::select(jsonb_object::<Array<Text>, _>(vec!["hello","world","John"]))
    ///                 .get_result::<Value>(connection);
    /// assert!(jsonb.is_err());
    ///
    /// let jsonb = diesel::select(jsonb_object::<Nullable<Array<Text>>, _>(None::<Vec<String>>))
    ///                 .get_result::<Option<Value>>(connection);
    /// assert!(jsonb.is_err());
    ///
    /// let empty:Vec<String> = Vec::new();
    /// let jsonb = diesel::select(jsonb_object::<Array<Nullable<Text>>,_>(empty))
    ///                 .get_result::<Value>(connection);
    /// assert!(jsonb.is_err());
    ///
    /// #     Ok(())
    /// # }
    /// ```
    fn jsonb_object<Arr: TextArrayOrNullableTextArray + MaybeNullableValue<Jsonb>>(
        text_array: Arr,
    ) -> Arr::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
    /// This form of jsonb_object takes keys and values pairwise from two separate arrays.
    /// In all other respects it is identical to the one-argument form.
    ///
    /// # Example
    ///
    /// ```rust
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {

Review comment (suggestion):
That does seem to be not correct, this should return `None` and no error.

Code after:
    ///
    /// ```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::json_array_length;
    /// #     use serde_json::{json, Value};
    /// #     use diesel::sql_types::{Integer, Json, Nullable};
    /// #     let connection = &mut establish_connection();
    ///
    /// let result = diesel::select(json_array_length::<Json, _>(json!([1, 2, 3])))
    ///     .get_result::<i32>(connection)?;
    /// assert_eq!(result, 3);
    ///
    /// let result = diesel::select(json_array_length::<Json, _>(json!([])))
    ///     .get_result::<i32>(connection)?;
    /// assert_eq!(result, 0);
    ///
    /// let result = diesel::select(json_array_length::<Nullable<Json>, _>(None::<Value>))
    ///     .get_result::<Option<i32>>(connection)?;
    /// assert!(result.is_none());
    ///
    ///
    ///
    /// #     Ok(())
    /// # }
    /// ```

    fn json_array_length<E: JsonOrNullableJson + MaybeNullableValue<Integer>>(json: E) -> E::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
    /// Returns the number of elements in the top-level JSON array
    ///
    ///
    /// # Example
    ///
    /// ```rust
    /// # include!("../../doctest_setup.rs");
    /// #
    /// # fn main() {
    /// #     #[cfg(feature = "serde_json")]
    /// #     run_test().unwrap();
    /// # }