-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6e9b27
commit 9f287d2
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::decode::Decode; | ||
use crate::encode::Encode; | ||
use crate::mysql::database::MySql; | ||
use crate::mysql::protocol::TypeId; | ||
use crate::mysql::types::*; | ||
use crate::mysql::{MySqlTypeInfo, MySqlValue}; | ||
use crate::types::{Json, Type}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value as JsonValue; | ||
|
||
impl Type<MySql> for JsonValue { | ||
fn type_info() -> MySqlTypeInfo { | ||
<Json<Self> as Type<MySql>>::type_info() | ||
} | ||
} | ||
|
||
impl Encode<MySql> for JsonValue { | ||
fn encode(&self, buf: &mut Vec<u8>) { | ||
Json(self).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 | ||
MySqlTypeInfo::new(TypeId::CHAR) | ||
} | ||
} | ||
|
||
impl<T> Encode<MySql> for Json<T> | ||
where | ||
T: Serialize, | ||
{ | ||
fn encode(&self, buf: &mut Vec<u8>) { | ||
let json_string_value = | ||
serde_json::to_string(&self.0).expect("serde_json failed to convert to string"); | ||
<str as Encode<MySql>>::encode(json_string_value.as_str(), buf); | ||
} | ||
} | ||
|
||
impl<'de, T> Decode<'de, MySql> for Json<T> | ||
where | ||
T: 'de, | ||
T: for<'de1> Deserialize<'de1>, | ||
{ | ||
fn decode(value: MySqlValue<'de>) -> crate::Result<Self> { | ||
let string_value = <&'de str as Decode<MySql>>::decode(value).unwrap(); | ||
serde_json::from_str(&string_value) | ||
.map(Json) | ||
.map_err(crate::Error::decode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters