From e7e2f5b37ae06cedfece37a03b2cc3a345602e37 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 19 Jun 2020 01:42:32 -0700 Subject: [PATCH] fix: implement transparent Serialize and Deserialize for Json<_> --- sqlx-core/src/types/json.rs | 4 +++- tests/mysql/types.rs | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/sqlx-core/src/types/json.rs b/sqlx-core/src/types/json.rs index 9aab1c62..60a95d4c 100644 --- a/sqlx-core/src/types/json.rs +++ b/sqlx-core/src/types/json.rs @@ -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(pub T); impl Deref for Json { diff --git a/tests/mysql/types.rs b/tests/mysql/types.rs index 5d8f4b69..3219c45d 100644 --- a/tests/mysql/types.rs +++ b/tests/mysql/types.rs @@ -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>, @@ -149,6 +152,6 @@ mod json_tests { test_type!(json_struct_json_column>( 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]) }) )); }