How to use JsonRawValue
?
#2306
Answered
by
nicksrandall
nicksrandall
asked this question in
Q&A
-
I'm making a query (Postgres) in a REST endpoint handler that returns a single column that is a JSON value. I would like to return this JSON value as the response of the endpoint and avoid the cost of deserializing and re-serializing the value before it is sent. I'm assuming that that is what Any help would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Answered by
nicksrandall
Jan 16, 2023
Replies: 1 comment
-
I finally figured it out: // this is the value that the query returns (using query_as)
#[derive(Deserialize)]
struct QueryResponse {
data: Box<sqlx::types::JsonRawValue>,
}
// custom impl of `FromRow`
impl FromRow<'_, PgRow> for QueryResponse {
fn from_row(row: &PgRow) -> sqlx::Result<Self> {
Ok(QueryResponse {
data: to_raw_value::<&sqlx::types::JsonRawValue>(&row.get(0)).unwrap(),
})
}
}
// This is what the api returns.
#[derive(Serialize)]
struct APIResponse {
data: Box<sqlx::types::JsonRawValue>,
// other fields omitted for brevity.
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
nicksrandall
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I finally figured it out: