Skip to content

Add more JsonRawValue encode/decode impls. #3859

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions sqlx-core/src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,59 @@ where
}
}

// We don't have to implement Encode for JsonRawValue because that's covered by the default
// implementation for Encode
impl<DB> Type<DB> for Box<JsonRawValue>
where
for<'a> Json<&'a Self>: Type<DB>,
DB: Database,
{
fn type_info() -> DB::TypeInfo {
<Json<&Self> as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
<Json<&Self> as Type<DB>>::compatible(ty)
}
}

impl<'q, DB> Encode<'q, DB> for JsonRawValue
where
for<'a> Json<&'a Self>: Encode<'q, DB>,
DB: Database,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<Json<&Self> as Encode<'q, DB>>::encode(Json(self), buf)
}
}

impl<'q, DB> Encode<'q, DB> for &'q JsonRawValue
where
for<'a> Json<&'a Self>: Encode<'q, DB>,
DB: Database,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<Json<&Self> as Encode<'q, DB>>::encode(Json(self), buf)
}
}

impl<'q, DB> Encode<'q, DB> for Box<JsonRawValue>
where
for<'a> Json<&'a Self>: Encode<'q, DB>,
DB: Database,
{
fn encode_by_ref(
&self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, BoxDynError> {
<Json<&Self> as Encode<'q, DB>>::encode(Json(self), buf)
}
}

impl<'r, DB> Decode<'r, DB> for &'r JsonRawValue
where
Json<Self>: Decode<'r, DB>,
Expand All @@ -207,3 +258,13 @@ where
<Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
}
}

impl<'r, DB> Decode<'r, DB> for Box<JsonRawValue>
where
Json<Self>: Decode<'r, DB>,
DB: Database,
{
fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
<Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
}
}
4 changes: 4 additions & 0 deletions tests/postgres/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ mod json {
.await?;

let value: &JsonRawValue = row.try_get(0)?;
assert_eq!(value.get(), "{\"hello\": \"world\"}");

let value: Box<JsonRawValue> = row.try_get(0)?;
assert_eq!(value.get(), "{\"hello\": \"world\"}");

// prepared, binary API
Expand All @@ -474,7 +476,9 @@ mod json {
.await?;

let value: &JsonRawValue = row.try_get(0)?;
assert_eq!(value.get(), "{\"hello\": \"world\"}");

let value: Box<JsonRawValue> = row.try_get(0)?;
assert_eq!(value.get(), "{\"hello\": \"world\"}");

Ok(())
Expand Down