Skip to content

Commit

Permalink
Merge pull request #4254 from anna-ahmed19/add_jsonb_pretty
Browse files Browse the repository at this point in the history
`jsonb_pretty` function
  • Loading branch information
weiznich authored Sep 13, 2024
2 parents 97898ec + 9300854 commit 2d34564
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
77 changes: 77 additions & 0 deletions diesel/src/pg/expression/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,3 +1756,80 @@ define_sql_function! {
/// ```
fn jsonb_typeof<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out;
}

#[cfg(feature = "postgres_backend")]
define_sql_function! {
/// Converts the given json value to pretty-printed, indented text
///
/// # 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::jsonb_pretty;
/// # use serde_json::{json, Value};
/// # use diesel::sql_types::{Jsonb, Nullable};
/// # let connection = &mut establish_connection();
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!([{"f1":1,"f2":null},2,null,3])))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"[
/// {
/// "f1": 1,
/// "f2": null
/// },
/// 2,
/// null,
/// 3
/// ]"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!({"a": 1, "b": "cd"})))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"{
/// "a": 1,
/// "b": "cd"
/// }"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!("abc")))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#""abc""#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!(22)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"22"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!(false)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"false"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!(null)))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"null"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Jsonb, _>(json!({})))
/// .get_result::<String>(connection)?;
///
/// assert_eq!(r#"{
/// }"#, result);
///
/// let result = diesel::select(jsonb_pretty::<Nullable<Jsonb>, _>(None::<Value>))
/// .get_result::<Option<String>>(connection)?;
///
/// assert!(result.is_none());
/// # Ok(())
/// # }
/// ```
fn jsonb_pretty<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out;
}
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 @@ -496,3 +496,8 @@ pub type json_typeof<E> = super::functions::json_typeof<SqlTypeOf<E>, E>;
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_typeof<E> = super::functions::jsonb_typeof<SqlTypeOf<E>, E>;

/// Return type of [`jsonb_pretty(jsonb)`](super::functions::jsonb_pretty())
#[allow(non_camel_case_types)]
#[cfg(feature = "postgres_backend")]
pub type jsonb_pretty<E> = super::functions::jsonb_pretty<SqlTypeOf<E>, E>;
1 change: 1 addition & 0 deletions diesel_derives/tests/auto_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ fn postgres_functions() -> _ {
json_object(pg_extras::text_array),
json_typeof(pg_extras::json),
jsonb_typeof(pg_extras::jsonb),
jsonb_pretty(pg_extras::jsonb),
)
}

Expand Down

0 comments on commit 2d34564

Please sign in to comment.