diff --git a/sqlx-core/src/types/json.rs b/sqlx-core/src/types/json.rs index fa187f4aab..f9d95c9b01 100644 --- a/sqlx-core/src/types/json.rs +++ b/sqlx-core/src/types/json.rs @@ -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 Type for Box +where + for<'a> Json<&'a Self>: Type, + DB: Database, +{ + fn type_info() -> DB::TypeInfo { + as Type>::type_info() + } + + fn compatible(ty: &DB::TypeInfo) -> bool { + as Type>::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 ::ArgumentBuffer<'q>, + ) -> Result { + 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 ::ArgumentBuffer<'q>, + ) -> Result { + as Encode<'q, DB>>::encode(Json(self), buf) + } +} + +impl<'q, DB> Encode<'q, DB> for Box +where + for<'a> Json<&'a Self>: Encode<'q, DB>, + DB: Database, +{ + fn encode_by_ref( + &self, + buf: &mut ::ArgumentBuffer<'q>, + ) -> Result { + as Encode<'q, DB>>::encode(Json(self), buf) + } +} + impl<'r, DB> Decode<'r, DB> for &'r JsonRawValue where Json: Decode<'r, DB>, @@ -207,3 +258,13 @@ where as Decode>::decode(value).map(|item| item.0) } } + +impl<'r, DB> Decode<'r, DB> for Box +where + Json: Decode<'r, DB>, + DB: Database, +{ + fn decode(value: ::ValueRef<'r>) -> Result { + as Decode>::decode(value).map(|item| item.0) + } +} diff --git a/tests/postgres/types.rs b/tests/postgres/types.rs index d5d34bc1b3..0ae0ac5c00 100644 --- a/tests/postgres/types.rs +++ b/tests/postgres/types.rs @@ -465,7 +465,9 @@ mod json { .await?; let value: &JsonRawValue = row.try_get(0)?; + assert_eq!(value.get(), "{\"hello\": \"world\"}"); + let value: Box = row.try_get(0)?; assert_eq!(value.get(), "{\"hello\": \"world\"}"); // prepared, binary API @@ -474,7 +476,9 @@ mod json { .await?; let value: &JsonRawValue = row.try_get(0)?; + assert_eq!(value.get(), "{\"hello\": \"world\"}"); + let value: Box = row.try_get(0)?; assert_eq!(value.get(), "{\"hello\": \"world\"}"); Ok(())