diesel::pg::expression::dsl

Function jsonb_typeof

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

Returns the type of the top-level jsonb value as a text-string

ยงExample

let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!({"a": "b", "c": 1})))
    .get_result::<String>(connection)?;

assert_eq!("object".to_string(), result);

let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!([1,2,3])))
    .get_result::<String>(connection)?;

assert_eq!("array".to_string(), result);

let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!("abc")))
    .get_result::<String>(connection)?;

assert_eq!("string".to_string(), result);

let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(-123.4)))
    .get_result::<String>(connection)?;

assert_eq!("number".to_string(), result);

let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(true)))
    .get_result::<String>(connection)?;

assert_eq!("boolean".to_string(), result);

let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(null)))
    .get_result::<String>(connection)?;

assert_eq!("null".to_string(), result);

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

assert!(result.is_none());