diesel::pg::expression::dsl

Function array_to_json

source
pub fn array_to_json<Arr: ArrayOrNullableArray + MaybeNullableValue<Json>, array>(
    array: array,
) -> array_to_json<Arr, array>
where array: AsExpression<Arr>,
Available on crate feature postgres_backend only.
Expand description

Converts any Array to json.

ยงExample

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