Rust use Postgres JSON属性:无法在Rust类型'alloc::string::string'和Postgres类型'jsonb'之间转换`
目前我可以使用以下代码,但我不想在postgres查询中强制转换JSON为文本,因为这会增加延迟Rust use Postgres JSON属性:无法在Rust类型'alloc::string::string'和Postgres类型'jsonb'之间转换`,json,postgresql,rust,rust-tokio,Json,Postgresql,Rust,Rust Tokio,目前我可以使用以下代码,但我不想在postgres查询中强制转换JSON为文本,因为这会增加延迟 async fn reverse_geocode(min : f32, max : f32, pool: &Pool) -> Result<String, PoolError> { let client: Client = pool.get().await?; let sql = format!("select \"json\"
async fn reverse_geocode(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))
}
可以使用什么类型来返回json值而不将其转换为文本?为了使用json和Jsonb值,您需要在postgres create with中启用该功能 功能=[with-serde_json-1] 然后您可以将返回类型更改为结果 那么你的货物里就有了
[dependencies]
postgres = {version = "0.17.3" , features = ["with-serde_json-1"] }
serde_json = "1.0.56"
在你的主要房间里
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))
}
为了使用Json和Jsonb值,您需要在postgres create with中启用该功能 功能=[with-serde_json-1] 然后您可以将返回类型更改为结果 那么你的货物里就有了
[dependencies]
postgres = {version = "0.17.3" , features = ["with-serde_json-1"] }
serde_json = "1.0.56"
在你的主要房间里
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))
}