Currently I am able to use the following code, but I do not want to have to cast my JSON to text in my postgres query since it adds latency.
async fn filter_data(min : f32, max : f32, pool: &Pool) -> Result<String, PoolError> {
let client: Client = pool.get().await?;
let sql = format!("select \"json\"::TEXT from get_data({}, {})", min, max);
let stmt = client.prepare(&sql).await?;
let rows = client.query(&stmt, &[]).await?;
Ok(rows[0].get(0))
}
If I do not cast my JSON to text, I get the following error:
error retrieving column 0: error deserializing column 0: cannot convert between the Rust type `alloc::string::String` and the Postgres type `jsonb`
What type can be used so that I return that json value without casting it to text?
In order to use Json and Jsonb values, you need to enable the feature in the postgres create with
features = ["with-serde_json-1"]
And then you can change your return type to be Result<serde_json::Value,PoolError>
so you would have in your cargo.toml
[dependencies]
postgres = {version = "0.17.3" , features = ["with-serde_json-1"] }
serde_json = "1.0.56"
and in your main.rs
async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result<serde_json::Value, PoolError> {
let client: Client = pool.get().await?;
let sql = format!("select \"json\" from get_data({}, {})", min, max);
let stmt = client.prepare(&sql).await?;
let rows = client.query(&stmt, &[]).await?;
Ok(rows[0].get(0))
}