Skip to content

Commit

Permalink
Merge pull request #4301 from meeshal/add_array_to_json_support
Browse files Browse the repository at this point in the history
add pg array_to_json support
  • Loading branch information
weiznich authored Oct 12, 2024
2 parents 9ed7de2 + da5486b commit a3ea524
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,49 @@ define_sql_function! {
fn array_sample<Arr: ArrayOrNullableArray + SingleValue>(array: Arr, n: Integer) -> Arr;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Converts any Array 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::array_to_json;
/// # use diesel::sql_types::{Array, Integer, Text, Nullable};
/// # use serde_json::Value;
/// # let connection = &mut establish_connection();
/// let json = diesel::select(array_to_json::<Array<Integer>, _>(vec![1, 2, 3, 4, 5]))
/// .get_result::<Value>(connection)?;
/// let expected:Value = serde_json::json!([1, 2, 3, 4, 5]);
/// assert_eq!(expected,json);
/// let json = diesel::select(array_to_json::<Array<Text>,_>(vec!["hello","world","John","Doe"]))
/// .get_result::<Value>(connection)?;
/// let expected:Value = serde_json::json!(["hello","world","John","Doe"]);
/// assert_eq!(expected,json);
/// let empty:Vec<String> = Vec::new();
/// let json = diesel::select(array_to_json::<Array<Nullable<Text>>,_>(empty))
/// .get_result::<Value>(connection)?;
/// assert_eq!(serde_json::json!([]),json);
/// let json = diesel::select(array_to_json::<Nullable<Array<Integer>>, _>(None::<Vec<i32>>))
/// .get_result::<Option<Value>>(connection)?;
/// assert_eq!(None, json);
/// # Ok(())
/// # }
/// ```
fn array_to_json<Arr: ArrayOrNullableArray + MaybeNullableValue<Json>>(
array: Arr,
) -> Arr::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Converts any SQL value to json
Expand Down
5 changes: 5 additions & 0 deletions diesel/src/pg/expression/helper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ pub type array_shuffle<A> = super::functions::array_shuffle<SqlTypeOf<A>, A>;
#[cfg(feature = "postgres_backend")]
pub type array_sample<A, N> = super::functions::array_sample<SqlTypeOf<A>, A, N>;

/// Return type of [`array_to_json(array)`](super::functions::array_to_json())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type array_to_json<A> = super::functions::array_to_json<SqlTypeOf<A>, A>;

/// Return type of [`to_json(element)`](super::functions::to_json())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
Expand Down
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ fn postgres_functions() -> _ {
array_ndims(pg_extras::array),
array_shuffle(pg_extras::array),
array_sample(pg_extras::array, pg_extras::id),
array_to_json(pg_extras::array),
to_json(pg_extras::id),
to_jsonb(pg_extras::id),
json_object(pg_extras::text_array),
Expand Down

0 comments on commit a3ea524

Please sign in to comment.