diesel::pg::expression::dsl

Function jsonb_pretty

source
pub fn jsonb_pretty<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>, e>(
    e: e,
) -> jsonb_pretty<E, e>
where e: AsExpression<E>,
Available on crate feature postgres_backend only.
Expand description

Converts the given json value to pretty-printed, indented text

ยงExample

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());