postgrest-rs icon indicating copy to clipboard operation
postgrest-rs copied to clipboard

Support json as parameter

Open jobot0 opened this issue 2 years ago • 3 comments

Hello!

Thanks for the amazing work :) Would it be possible to add Serde json support to perform queries based on json input?

jobot0 avatar Apr 28 '22 13:04 jobot0

Hmm, maybe we should instead be doing insert_tostring where T: ToString? That makes it more general and doesn't lock us down to serde.

But in that case it's not difficult to simply run to_string() on the value and pass it to insert, e.g.

let x = json!(r#"{"hello":"world"}"#);
let resp = client
    .from("table")
    .insert(x.to_string())
// ...

soedirgo avatar Apr 29 '22 08:04 soedirgo

Yep you're right it would lock us down to serde. However, should we prefer making the manipulation on user side either? Sorry, I'm quite new to Rust idioms and rules. Thus I was a bit surprised as the underlying code seems to be based on reqwest which accepts json with Serialize trait. Once again sorry if I misunderstood the codebase intention or implementation 😅

jobot0 avatar Apr 29 '22 11:04 jobot0

No probs!

There might be more upside to enabling it in reqwest - e.g. you otherwise need to add a Content-Type: application/json header, which doesn't apply here because we implicitly set that header.

reqwest also converts the json to Vec<u8> instead of converting it to String first (we might want to do this as well). Since we're doing to_string() in insert_json anyway, I find it cleaner to just use to_string() when passing the argument (just a few extra keystrokes), which also works with e.g. miniserde.

soedirgo avatar May 04 '22 08:05 soedirgo