1

I am using r2d2_postgres to run raw SQL and return the result directly in Actix response.

I need to run raw sql with dynamic data, types and unknown at compile time columns/data types etc. To do this, I came across serde_json::value::RawValue:

It states: "A RawValue can be used to defer parsing parts of a payload until later, or to avoid parsing it at all in the case that part of the payload just needs to be transferred verbatim into a different output object."

https://docs.serde.rs/serde_json/value/struct.RawValue.html

I basically want to avoid parsing it at all and just send it to the Actix client.

Here's my code:

let mut conn = pool.get().expect("Couldn't get db connection from pool");
let result = web::block(move || conn.query("select jsonb_agg(t) from (select * from useraccountview) t",&[]))
        .await;

match result {
        Ok(result) => {
            let foo : serde_json::value::RawValue = result[0].get(0);
            HttpResponse::Ok().content_type("application/json").json(foo)
        },
        Err(e) => HttpResponse::NotAcceptable().body(e.to_string())
    }

This gives me 2 errors:

error[E0277]: the trait bound `serde_json::value::RawValue: FromSql<'_>` is not satisfied
   --> src/main.rs:188:63
    |
188 |             let foo : serde_json::value::RawValue = result[0].get(0);
    |                                                               ^^^ the trait `FromSql<'_>` is not implemented for `serde_json::value::RawValue`



error[E0277]: the size for values of type `str` cannot be known at compilation time
   --> src/main.rs:188:17
    |
188 |             let foo : serde_json::value::RawValue = result[0].get(0);
    |                 ^^^ doesn't have a size known at compile-time

I am very new to Rust so I am probably missing some concept on how to fix this.

  • 1
    Should be able to use [`Json`](https://docs.rs/postgres/0.19.0/postgres/types/struct.Json.html)`<&RawValue>` or something – kmdreko Jan 27 '21 at 01:59

0 Answers0