From 99d3220d8888c67b81bc74fa561686c9e8dc6604 Mon Sep 17 00:00:00 2001 From: "Andre B. Reis" Date: Wed, 23 Feb 2022 20:57:48 +0000 Subject: [PATCH] Impl PgHasArrayType for serde_json::{Value,RawValue} (#1722) * Implement PgHasArrayType for JSON types in the serde_json crate * Remove redundant Type impls for arrays and Vecs of JsonValue * Relax an implicit Sized bound to support JsonRawValue --- sqlx-core/src/postgres/types/json.rs | 22 ++++++++++++++++++++++ sqlx-core/src/postgres/types/mod.rs | 2 +- sqlx-core/src/types/json.rs | 28 ---------------------------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/sqlx-core/src/postgres/types/json.rs b/sqlx-core/src/postgres/types/json.rs index d7228169..523cd297 100644 --- a/sqlx-core/src/postgres/types/json.rs +++ b/sqlx-core/src/postgres/types/json.rs @@ -7,6 +7,8 @@ use crate::postgres::{ }; use crate::types::{Json, Type}; use serde::{Deserialize, Serialize}; +use serde_json::value::RawValue as JsonRawValue; +use serde_json::Value as JsonValue; // @@ -34,6 +36,26 @@ impl PgHasArrayType for Json { } } +impl PgHasArrayType for JsonValue { + fn array_type_info() -> PgTypeInfo { + PgTypeInfo::JSONB_ARRAY + } + + fn array_compatible(ty: &PgTypeInfo) -> bool { + array_compatible::(ty) + } +} + +impl PgHasArrayType for JsonRawValue { + fn array_type_info() -> PgTypeInfo { + PgTypeInfo::JSONB_ARRAY + } + + fn array_compatible(ty: &PgTypeInfo) -> bool { + array_compatible::(ty) + } +} + impl<'q, T> Encode<'q, Postgres> for Json where T: Serialize, diff --git a/sqlx-core/src/postgres/types/mod.rs b/sqlx-core/src/postgres/types/mod.rs index 19a8165f..e0a542fd 100644 --- a/sqlx-core/src/postgres/types/mod.rs +++ b/sqlx-core/src/postgres/types/mod.rs @@ -231,7 +231,7 @@ pub use time_tz::PgTimeTz; pub use record::{PgRecordDecoder, PgRecordEncoder}; // Type::compatible impl appropriate for arrays -fn array_compatible>(ty: &PgTypeInfo) -> bool { +fn array_compatible + ?Sized>(ty: &PgTypeInfo) -> bool { // we require the declared type to be an _array_ with an // element type that is acceptable if let PgTypeKind::Array(element) = &ty.kind() { diff --git a/sqlx-core/src/types/json.rs b/sqlx-core/src/types/json.rs index 5d3a74c3..11b29a09 100644 --- a/sqlx-core/src/types/json.rs +++ b/sqlx-core/src/types/json.rs @@ -56,34 +56,6 @@ where } } -impl Type for Vec -where - Vec>: Type, - DB: Database, -{ - fn type_info() -> DB::TypeInfo { - > as Type>::type_info() - } - - fn compatible(ty: &DB::TypeInfo) -> bool { - > as Type>::compatible(ty) - } -} - -impl Type for [JsonValue] -where - [Json]: Type, - DB: Database, -{ - fn type_info() -> DB::TypeInfo { - <[Json] as Type>::type_info() - } - - fn compatible(ty: &DB::TypeInfo) -> bool { - <[Json] as Type>::compatible(ty) - } -} - impl<'q, DB> Encode<'q, DB> for JsonValue where for<'a> Json<&'a Self>: Encode<'q, DB>,