diesel::pg::expression::dsl

Function json_populate_record

Source
pub fn json_populate_record<B: RecordOrNullableRecord + SingleValue, J: JsonOrNullableJson + CombinedAllNullableValue<Json, B>, base, from_json>(
    base: base,
    from_json: from_json,
) -> json_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 json_populate_record takes a Record base and Json 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(json_populate_record::<Record<(Text, Integer)>, Json, _, _>(
        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(json_populate_record::<Record<(Text, Integer)>, Json, _, _>(
        sql::<Record<(Text, Integer)>>("ROW('John', 30)"),
        expected
)).get_result(connection)?;
assert_eq!(record, ("John".to_string(), 30));

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