diesel::pg::expression::dsl

Function to_jsonb

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

Converts any SQL value to jsonb

ยงExample

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

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

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

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

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

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

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

assert!(result.is_none());