diesel::sqlite::expression::dsl

Function jsonb

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

The jsonb(X) function returns the binary JSONB representation of the JSON provided as argument X.

ยงExample


let version = diesel::select(sql::<Text>("sqlite_version();"))
        .get_result::<String>(connection)?;

// Querying SQLite version should not fail.
let version_components: Vec<&str> = version.split('.').collect();
let major: u32 = version_components[0].parse().unwrap();
let minor: u32 = version_components[1].parse().unwrap();
let patch: u32 = version_components[2].parse().unwrap();

if major > 3 || (major == 3 && minor >= 45) {
    /* Valid sqlite version, do nothing */
} else {
    println!("SQLite version is too old, skipping the test.");
    return Ok(());
}

let result = diesel::select(jsonb::<Binary, _>(br#"{"a": "b", "c": 1}"#))
    .get_result::<Value>(connection)?;

assert_eq!(json!({"a": "b", "c": 1}), result);

let result = diesel::select(jsonb::<Binary, _>(br#"{"this":"is","a":["test"]}"#))
    .get_result::<Value>(connection)?;

assert_eq!(json!({"this":"is","a":["test"]}), result);

let result = diesel::select(jsonb::<Nullable<Binary>, _>(None::<Vec<u8>>))
    .get_result::<Option<Value>>(connection)?;

assert!(result.is_none());