Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

396
Vistas
Rust use Postgres JSON attribute: cannot convert between the Rust type `alloc::string::String` and the Postgres type `jsonb`

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 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))
}

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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))
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda