diesel::pg::expression::dsl

Function to_json

Source
pub fn to_json<E: MaybeNullableValue<Json>, e>(e: e) -> to_json<E, e>
where e: AsExpression<E>,
Available on crate feature postgres_backend only.
Expand description

Converts any SQL value to json

ยงExample

let result = diesel::select(to_json::<Integer, _>(1))
    .get_result::<Value>(connection)?;

assert_eq!(json!(1), result);

let result = diesel::select(to_json::<Array<Text>, _>(vec!["abc", "xyz"]))
    .get_result::<Value>(connection)?;

assert_eq!(json!(["abc", "xyz"]), result);

let result = diesel::select(to_json::<Array<Nullable<Text>>, _>(Vec::<String>::new()))
    .get_result::<Value>(connection)?;

assert_eq!(json!([]), result);

let result = diesel::select(to_json::<Nullable<Text>, _>(None::<String>))
    .get_result::<Option<Value>>(connection)?;

assert!(result.is_none());