Skip to content

Commit 2b493e5

Browse files
json: Refactor Encode/Decode for serde values to database agnostic implementation
Since the implementation of Encode and Decode for both mysql and postgres on serde's Value and RawValue were practically the same they were moved to the generic json module.
1 parent 41e56ce commit 2b493e5

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

Cargo.lock

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sqlx-core/src/mysql/types/json.rs

-12
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ impl Type<MySql> for JsonValue {
1414
}
1515
}
1616

17-
impl Encode<MySql> for JsonValue {
18-
fn encode(&self, buf: &mut Vec<u8>) {
19-
(Box::new(Json(self)) as Box<dyn Encode<MySql>>).encode(buf)
20-
}
21-
}
22-
23-
impl<'de> Decode<'de, MySql> for JsonValue {
24-
fn decode(value: MySqlValue<'de>) -> crate::Result<Self> {
25-
<Json<Self> as Decode<MySql>>::decode(value).map(|item| item.0)
26-
}
27-
}
28-
2917
impl<T> Type<MySql> for Json<T> {
3018
fn type_info() -> MySqlTypeInfo {
3119
// MySql uses the CHAR type to pass JSON data from and to the client

sqlx-core/src/postgres/types/json.rs

-24
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,12 @@ impl Type<Postgres> for JsonValue {
2121
}
2222
}
2323

24-
impl Encode<Postgres> for JsonValue {
25-
fn encode(&self, buf: &mut PgRawBuffer) {
26-
(Box::new(Json(self)) as Box<dyn Encode<Postgres>>).encode(buf)
27-
}
28-
}
29-
30-
impl<'de> Decode<'de, Postgres> for JsonValue {
31-
fn decode(value: PgValue<'de>) -> crate::Result<Self> {
32-
<Json<Self> as Decode<Postgres>>::decode(value).map(|item| item.0)
33-
}
34-
}
35-
3624
impl Type<Postgres> for &'_ JsonRawValue {
3725
fn type_info() -> PgTypeInfo {
3826
<Json<Self> as Type<Postgres>>::type_info()
3927
}
4028
}
4129

42-
impl Encode<Postgres> for &'_ JsonRawValue {
43-
fn encode(&self, buf: &mut PgRawBuffer) {
44-
(Box::new(Json(self)) as Box<dyn Encode<Postgres>>).encode(buf)
45-
}
46-
}
47-
48-
impl<'de> Decode<'de, Postgres> for &'de JsonRawValue {
49-
fn decode(value: PgValue<'de>) -> crate::Result<Self> {
50-
<Json<Self> as Decode<Postgres>>::decode(value).map(|item| item.0)
51-
}
52-
}
53-
5430
impl<T> Type<Postgres> for Json<T> {
5531
fn type_info() -> PgTypeInfo {
5632
PgTypeInfo::new(TypeId::JSONB, "JSONB")

sqlx-core/src/types.rs

+38
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ pub mod ipnetwork {
4343

4444
#[cfg(feature = "json")]
4545
pub mod json {
46+
use crate::database::Database;
47+
use crate::decode::Decode;
48+
use crate::encode::Encode;
49+
use crate::value::HasRawValue;
50+
use serde_json::value::RawValue as JsonRawValue;
51+
use serde_json::Value as JsonValue;
4652
use std::ops::Deref;
4753

4854
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
@@ -62,6 +68,38 @@ pub mod json {
6268
&self.0
6369
}
6470
}
71+
72+
impl<DB> Encode<DB> for JsonValue
73+
where
74+
Json<Self>: Encode<DB>,
75+
DB: Database,
76+
{
77+
fn encode(&self, buf: &mut DB::RawBuffer) {
78+
<Json<Self> as Encode<DB>>::encode(&Json(self.clone()), buf)
79+
}
80+
}
81+
82+
impl<'de, DB> Decode<'de, DB> for JsonValue
83+
where
84+
Json<Self>: Decode<'de, DB>,
85+
DB: Database,
86+
{
87+
fn decode(value: <DB as HasRawValue<'de>>::RawValue) -> crate::Result<Self> {
88+
<Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
89+
}
90+
}
91+
92+
// We don't have to implement Encode for JsonRawValue because that's covered by the default
93+
// implementation for Encode
94+
impl<'de, DB> Decode<'de, DB> for &'de JsonRawValue
95+
where
96+
Json<Self>: Decode<'de, DB>,
97+
DB: Database,
98+
{
99+
fn decode(value: <DB as HasRawValue<'de>>::RawValue) -> crate::Result<Self> {
100+
<Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
101+
}
102+
}
65103
}
66104
#[cfg(feature = "json")]
67105
pub use self::json::Json;

0 commit comments

Comments
 (0)