Skip to content

Commit

Permalink
json: Refactor Encode/Decode for serde values to be generic for DB
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
blackwolf12333 committed Apr 18, 2020
1 parent 41e56ce commit c96de85
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions sqlx-core/src/mysql/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ impl Type<MySql> for JsonValue {
}
}

impl Encode<MySql> for JsonValue {
fn encode(&self, buf: &mut Vec<u8>) {
(Box::new(Json(self)) as Box<dyn Encode<MySql>>).encode(buf)
}
}

impl<'de> Decode<'de, MySql> for JsonValue {
fn decode(value: MySqlValue<'de>) -> crate::Result<Self> {
<Json<Self> as Decode<MySql>>::decode(value).map(|item| item.0)
}
}

impl<T> Type<MySql> for Json<T> {
fn type_info() -> MySqlTypeInfo {
// MySql uses the CHAR type to pass JSON data from and to the client
Expand Down
24 changes: 0 additions & 24 deletions sqlx-core/src/postgres/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,12 @@ impl Type<Postgres> for JsonValue {
}
}

impl Encode<Postgres> for JsonValue {
fn encode(&self, buf: &mut PgRawBuffer) {
(Box::new(Json(self)) as Box<dyn Encode<Postgres>>).encode(buf)
}
}

impl<'de> Decode<'de, Postgres> for JsonValue {
fn decode(value: PgValue<'de>) -> crate::Result<Self> {
<Json<Self> as Decode<Postgres>>::decode(value).map(|item| item.0)
}
}

impl Type<Postgres> for &'_ JsonRawValue {
fn type_info() -> PgTypeInfo {
<Json<Self> as Type<Postgres>>::type_info()
}
}

impl Encode<Postgres> for &'_ JsonRawValue {
fn encode(&self, buf: &mut PgRawBuffer) {
(Box::new(Json(self)) as Box<dyn Encode<Postgres>>).encode(buf)
}
}

impl<'de> Decode<'de, Postgres> for &'de JsonRawValue {
fn decode(value: PgValue<'de>) -> crate::Result<Self> {
<Json<Self> as Decode<Postgres>>::decode(value).map(|item| item.0)
}
}

impl<T> Type<Postgres> for Json<T> {
fn type_info() -> PgTypeInfo {
PgTypeInfo::new(TypeId::JSONB, "JSONB")
Expand Down
38 changes: 38 additions & 0 deletions sqlx-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ pub mod ipnetwork {

#[cfg(feature = "json")]
pub mod json {
use crate::database::Database;
use crate::decode::Decode;
use crate::encode::Encode;
use crate::value::HasRawValue;
use serde_json::value::RawValue as JsonRawValue;
use serde_json::Value as JsonValue;
use std::ops::Deref;

#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
Expand All @@ -62,6 +68,38 @@ pub mod json {
&self.0
}
}

impl<DB> Encode<DB> for JsonValue
where
Json<Self>: Encode<DB>,
DB: Database,
{
fn encode(&self, buf: &mut DB::RawBuffer) {
<Json<Self> as Encode<DB>>::encode(&Json(self.clone()), buf)
}
}

impl<'de, DB> Decode<'de, DB> for JsonValue
where
Json<Self>: Decode<'de, DB>,
DB: Database,
{
fn decode(value: <DB as HasRawValue<'de>>::RawValue) -> crate::Result<Self> {
<Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
}
}

// We don't have to implement Encode for JsonRawValue because that's covered by the default
// implementation for Encode
impl<'de, DB> Decode<'de, DB> for &'de JsonRawValue
where
Json<Self>: Decode<'de, DB>,
DB: Database,
{
fn decode(value: <DB as HasRawValue<'de>>::RawValue) -> crate::Result<Self> {
<Json<Self> as Decode<DB>>::decode(value).map(|item| item.0)
}
}
}
#[cfg(feature = "json")]
pub use self::json::Json;
Expand Down

0 comments on commit c96de85

Please sign in to comment.