Actualmente puedo usar el siguiente código, pero no quiero tener que convertir mi JSON en texto en mi consulta de postgres, ya que agrega latencia.
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)) }Si no transfiero mi JSON a texto, obtengo el siguiente error:
error retrieving column 0: error deserializing column 0: cannot convert between the Rust type `alloc::string::String` and the Postgres type `jsonb`¿Qué tipo se puede usar para devolver ese valor json sin convertirlo en texto?
Para usar los valores Json y Jsonb, debe habilitar la función en la creación de postgres con features = ["with-serde_json-1"]
Y luego puede cambiar su tipo de devolución para que sea Result<serde_json::Value,PoolError>
por lo que tendría en su cargo.toml
[dependencies] postgres = {version = "0.17.3" , features = ["with-serde_json-1"] } serde_json = "1.0.56"y en tu 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)) }