diesel::pg::expression::dsl

Function jsonb_populate_record

Source
pub fn jsonb_populate_record<B: RecordOrNullableRecord + SingleValue, J: JsonbOrNullableJsonb + CombinedAllNullableValue<Jsonb, B>, base, from_json>(
    base: base,
    from_json: from_json,
) -> jsonb_populate_record<B, J, base, from_json>
where base: AsExpression<B>, from_json: AsExpression<J>,
Available on crate feature postgres_backend only.
Expand description

This function jsonb_populate_record takes a Record base and Jsonb as an input and converts it to top-level JSON object to a row having the composite type of the base argument.

ยงExample


let expected: Value = serde_json::json!({
    "f1": "Alice",
    "f2": 16
});
let record: (String, i32) = diesel::select(jsonb_populate_record::<Record<(Text, Integer)>, Jsonb, _, _>(
        sql::<Record<(Text, Integer)>>("ROW('John', 30)"),
        expected
)).get_result(connection)?;
assert_eq!(record, ("Alice".to_string(), 16));

let expected: Value = serde_json::json!({});
let record: (String, i32) = diesel::select(jsonb_populate_record::<Record<(Text, Integer)>, Jsonb, _, _>(
        sql::<Record<(Text, Integer)>>("ROW('John', 30)"),
        expected
)).get_result(connection)?;
assert_eq!(record, ("John".to_string(), 30));

let expected: Value = serde_json::json!({
    "f2": 42,
});
let record: (String, i32) = diesel::select(jsonb_populate_record::<Record<(Text, Integer)>, Jsonb, _, _>(
        sql::<Record<(Text, Integer)>>("ROW('John', 30)"),
        expected
)).get_result(connection)?;
assert_eq!(record, ("John".to_string(), 42));