diff --git a/diesel/src/pg/expression/expression_methods.rs b/diesel/src/pg/expression/expression_methods.rs index d3bcc81a9d5f..faa32d9f1a84 100644 --- a/diesel/src/pg/expression/expression_methods.rs +++ b/diesel/src/pg/expression/expression_methods.rs @@ -3418,9 +3418,11 @@ where pub(in crate::pg) mod private { use crate::sql_types::{ - Array, Binary, Cidr, Inet, Integer, Json, Jsonb, MaybeNullableType, Multirange, Nullable, - OneIsNullable, Range, SingleValue, SqlType, Text, + Array, Binary, Cidr, Inet, Integer, MaybeNullableType, Multirange, Nullable, OneIsNullable, + Range, SingleValue, SqlType, Text, }; + #[cfg(feature = "serde_json")] + use crate::sql_types::{Json, Jsonb}; use crate::{Expression, IntoSql}; /// Marker trait used to implement `ArrayExpressionMethods` on the appropriate @@ -3558,7 +3560,9 @@ pub(in crate::pg) mod private { )] pub trait JsonOrNullableJson {} + #[cfg(feature = "serde_json")] impl JsonOrNullableJson for Json {} + #[cfg(feature = "serde_json")] impl JsonOrNullableJson for Nullable {} /// A trait that describes valid json indices used by postgresql @@ -3646,9 +3650,13 @@ pub(in crate::pg) mod private { note = "try to provide an expression that produces one of the expected sql types" )] pub trait JsonOrNullableJsonOrJsonbOrNullableJsonb {} + #[cfg(feature = "serde_json")] impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Json {} + #[cfg(feature = "serde_json")] impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Nullable {} + #[cfg(feature = "serde_json")] impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Jsonb {} + #[cfg(feature = "serde_json")] impl JsonOrNullableJsonOrJsonbOrNullableJsonb for Nullable {} pub trait JsonIndex { diff --git a/diesel/src/pg/types/json.rs b/diesel/src/pg/types/json.rs index 9f43addf5e2e..f8603872b308 100644 --- a/diesel/src/pg/types/json.rs +++ b/diesel/src/pg/types/json.rs @@ -47,6 +47,7 @@ impl ToSql for serde_json::Value { } #[cfg(test)] +#[cfg(all(feature = "postgres", feature = "serde_json"))] mod tests { use crate::deserialize::FromSql; use crate::pg::{Pg, PgValue}; diff --git a/diesel/src/sql_types/mod.rs b/diesel/src/sql_types/mod.rs index 31ac664142b7..d581ab1b3e32 100644 --- a/diesel/src/sql_types/mod.rs +++ b/diesel/src/sql_types/mod.rs @@ -398,7 +398,7 @@ pub struct Timestamp; #[cfg(any( feature = "postgres_backend", feature = "mysql_backend", - all(feature = "sqlite", feature = "serde_json") + all(feature = "sqlite", feature = "serde_json",) ))] #[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] #[diesel(postgres_type(oid = 114, array_oid = 199))] @@ -479,21 +479,24 @@ pub struct Json; /// } /// } /// +/// # #[cfg(all( +/// # feature = "serde_json", +/// # any( +/// # feature = "postgres_backend", +/// # all(feature = "sqlite", feature = "returning_clauses_for_sqlite_3_35"), +/// # ) +/// # ))] /// # fn main() -> Result<(), Box> { /// # use diesel::insert_into; /// # use self::contacts::dsl::*; /// # let connection = &mut connection_no_data(); -/// # #[cfg(all(feature = "postgres_backend", feature = "serde_json"))] +/// # #[cfg(feature = "postgres_backend")] /// # diesel::sql_query("CREATE TABLE contacts ( /// # id SERIAL PRIMARY KEY, /// # name VARCHAR NOT NULL, /// # address JSONB NOT NULL /// # )").execute(connection)?; -/// # #[cfg(all( -/// # feature = "sqlite", -/// # feature = "serde_json", -/// # feature = "returning_clauses_for_sqlite_3_35" -/// # ))] +/// # #[cfg(feature = "sqlite")] /// # diesel::sql_query("CREATE TABLE contacts ( /// # id INT PRIMARY KEY, /// # name TEXT NOT NULL, @@ -512,12 +515,18 @@ pub struct Json; /// assert_eq!(santas_address, inserted_address); /// # Ok(()) /// # } -/// # #[cfg(not(feature = "serde_json"))] +/// # #[cfg(not(all( +/// # feature = "serde_json", +/// # any( +/// # feature = "postgres_backend", +/// # all(feature = "sqlite", feature = "returning_clauses_for_sqlite_3_35"), +/// # ) +/// # )))] /// # fn main() {} /// ``` -#[cfg(any( - feature = "postgres_backend", - all(feature = "sqlite", feature = "serde_json") +#[cfg(all( + feature = "serde_json", + any(feature = "postgres_backend", feature = "sqlite") ))] #[derive(Debug, Clone, Copy, Default, QueryId, SqlType)] #[diesel(postgres_type(oid = 3802, array_oid = 3807))] diff --git a/diesel/src/sqlite/types/json.rs b/diesel/src/sqlite/types/json.rs index 91fe45ee0b7f..cf8aac05bc68 100644 --- a/diesel/src/sqlite/types/json.rs +++ b/diesel/src/sqlite/types/json.rs @@ -19,12 +19,14 @@ const JSONB_TEXTRAW: u8 = 0x0A; const JSONB_ARRAY: u8 = 0x0B; const JSONB_OBJECT: u8 = 0x0C; +#[cfg(all(feature = "sqlite", feature = "serde_json"))] impl FromSql for serde_json::Value { fn from_sql(value: SqliteValue<'_, '_, '_>) -> deserialize::Result { serde_json::from_str(value.read_text()).map_err(|_| "Invalid Json".into()) } } +#[cfg(all(feature = "sqlite", feature = "serde_json"))] impl ToSql for serde_json::Value { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { out.set_value(serde_json::to_string(self)?); @@ -32,6 +34,7 @@ impl ToSql for serde_json::Value { } } +#[cfg(all(feature = "sqlite", feature = "serde_json"))] impl FromSql for serde_json::Value { fn from_sql(value: SqliteValue<'_, '_, '_>) -> deserialize::Result { let bytes = value.read_blob(); @@ -47,6 +50,7 @@ impl FromSql for serde_json::Value { } } +#[cfg(all(feature = "sqlite", feature = "serde_json"))] impl ToSql for serde_json::Value { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { // Create a buffer to hold the binary JSONB encoding @@ -451,6 +455,7 @@ fn write_jsonb_object( } #[cfg(test)] +#[cfg(all(feature = "postgres_backend", feature = "serde_json"))] mod tests { use super::*; use crate::query_dsl::RunQueryDsl; diff --git a/diesel/src/sqlite/types/mod.rs b/diesel/src/sqlite/types/mod.rs index 8f3235c96d0f..1fabfce5aac1 100644 --- a/diesel/src/sqlite/types/mod.rs +++ b/diesel/src/sqlite/types/mod.rs @@ -1,5 +1,5 @@ mod date_and_time; -#[cfg(all(feature = "sqlite", feature = "serde_json"))] +#[cfg(all(feature = "sqlite", feature = "serde_json",))] mod json; mod numeric; diff --git a/diesel/src/type_impls/json.rs b/diesel/src/type_impls/json.rs index 9d19c464fa90..57f2d1bc10e9 100644 --- a/diesel/src/type_impls/json.rs +++ b/diesel/src/type_impls/json.rs @@ -9,5 +9,10 @@ use crate::sql_types::Jsonb; #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = Json)] -#[cfg_attr(any(feature = "postgres_backend", feature = "sqlite"), diesel(sql_type = Jsonb))] +#[cfg_attr( + any( + feature = "postgres_backend", + feature = "sqlite" + ), +diesel(sql_type = Jsonb))] struct SerdeJsonValueProxy(serde_json::Value);