Closed Cariaga closed 10 months ago
You can parse the JSON string into a serde_json::Value with https://docs.rs/serde_json/latest/serde_json/fn.from_str.html
thank you
let mut query_params: Vec<Box<dyn postgres::types::ToSql + Sync>> = Vec::new();
let json_str = r#"
{
"name": "Alice",
"age": 25,
"is_student": true
}
"#;
let x: Value = serde_json::from_str(json_str).unwrap();
i guess the next issue now is
query_params.push(Box::new(x));
which is causing
error[E0277]: the trait bound `serde_json::Value: ToSql` is not satisfied
--> cache_proxy\src\socket\message\mod.rs:65:39
|
65 | query_params.push(Box::new(x));
| ^^^^^^^^^^^ the trait `ToSql` is not implemented for `serde_json::Value`
|
= help: the following other types implement trait `ToSql`:
bool
i8
i16
i32
i64
u32
f32
f64
and 18 others
= note: required for the cast from `Box<serde_json::Value>` to `Box<dyn ToSql + Sync>`
looks like i have to also add this in cargo.toml
postgres ={version="0.19.7", features = ["with-serde_json-1","with-chrono-0_4","array-impls","with-geo-types-0_7","with-time-0_3","with-uuid-1"]}
based on here https://github.com/sfackler/rust-postgres/blob/master/postgres/Cargo.toml i got confused because https://docs.rs/postgres/latest/postgres/types/trait.ToSql.html does not say which cargo features its referring to which i got the correct imports from https://github.com/sfackler/rust-postgres/blob/master/postgres/Cargo.toml
In addition, some implementations are provided for types in third party crates. These are disabled by default; to opt into one of these implementations, activate the Cargo feature corresponding to the crate’s name prefixed by with-. For example, the with-serde_json-1 feature enables the implementation for the serde_json::Value type.
thank you. Solved just wanted to include the postgres ={version="0.19.7", features = ["with-serde_json-1","with-chrono-0_4","array-impls","with-geo-types-0_7","with-time-0_3","with-uuid-1"]}
for future reference what the docs mean
i could not find an exact sample for jsonb,date,time