Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

postgresql - Rust serde_json::value::RawValue gives errors "the trait bound ... `r2d2_postgres::postgres::types::FromSql<'_>` is not satisfied"

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.

question from:https://stackoverflow.com/questions/65911419/rust-serde-jsonvaluerawvalue-gives-errors-the-trait-bound-r2d2-postgre

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...