diesel::pg::expression::dsl

Function row_to_json

Source
pub fn row_to_json<R: RecordOrNullableRecord + MaybeNullableValue<Json>, record>(
    record: record,
) -> row_to_json<R, record>
where record: AsExpression<R>,
Available on crate feature postgres_backend only.
Expand description

This function row_to_json takes a Record type as an input and converts it to JSON.

ยงExample


let json_value = diesel::select(row_to_json(sql::<Record<(Text, Integer)>>(
    "ROW('John', 30)"
)))
.get_result::<Value>(connection)?;
let expected: Value = serde_json::json!({
    "f1": "John",
    "f2": 30
});
assert_eq!(expected, json_value);

let json_value = diesel::select(row_to_json(sql::<Record<()>>("ROW()")))
.get_result::<Value>(connection)?;
let expected: Value = serde_json::json!({});
assert_eq!(expected, json_value);