fix: implement transparent Serialize and Deserialize for Json<_>

This commit is contained in:
Ryan Leckey 2020-06-19 01:42:32 -07:00
parent 93728a051c
commit e7e2f5b37a
2 changed files with 7 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use std::ops::Deref;
use serde::{Serialize, Deserialize};
use serde_json::value::RawValue as JsonRawValue;
use serde_json::Value as JsonValue;
@ -9,7 +10,8 @@ use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::types::Type;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Json<T>(pub T);
impl<T> Deref for Json<T> {

View File

@ -142,6 +142,9 @@ mod json_tests {
"\'{\"name\":\"Joe\",\"age\":33}\'" == Json(Friend { name: "Joe".to_string(), age: 33 })
));
// NOTE: This is testing recursive (and transparent) usage of the `Json` wrapper. You don't
// need to wrap the Vec in Json<_> to make the example work.
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
struct Customer {
json_column: Json<Vec<i64>>,
@ -149,6 +152,6 @@ mod json_tests {
test_type!(json_struct_json_column<Json<Customer>>(
MySql,
"\'{ \"json_column\": [1, 2] }\'" == Json(Customer { json_column: Json(vec![1, 2]) })
"\'{\"json_column\":[1,2]}\'" == Json(Customer { json_column: Json(vec![1, 2]) })
));
}