diesel::pg::expression::dsl

Function json_typeof

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

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

ยงExample

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

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

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

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

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

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

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

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

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

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

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

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

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

assert!(result.is_none());