Skip to content

How to use JsonRawValue? #2306

Answered by nicksrandall
nicksrandall asked this question in Q&A
Discussion options

You must be logged in to vote

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

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nicksrandall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant