@@ -158,6 +158,82 @@ pub trait PgExpressionMethods: Expression + Sized {
158
158
{
159
159
Grouped ( IsContainedBy :: new ( self , other. as_expression ( ) ) )
160
160
}
161
+
162
+ /// Creates a PostgreSQL `IS JSON` expression.
163
+ /// Requires PostgreSQL>=16
164
+ ///
165
+ /// This operator returns true whether an object is a valid JSON
166
+ ///
167
+ /// # Example
168
+ ///
169
+ /// ```rust,no_run
170
+ /// # include!("../../doctest_setup.rs");
171
+ /// #
172
+ /// # fn main() {
173
+ /// # run_test().unwrap();
174
+ /// # }
175
+ /// #
176
+ /// # fn run_test() -> QueryResult<()> {
177
+ /// # use std::collections::Bound;
178
+ /// # use diesel::sql_types::Text;
179
+ /// #
180
+ /// # let conn = &mut establish_connection();
181
+ /// #
182
+ ///
183
+ /// let res = diesel::select(("1".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
184
+ /// assert_eq!(res, true);
185
+ /// let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
186
+ /// assert_eq!(res, true);
187
+ /// let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
188
+ /// assert_eq!(res, true);
189
+ /// let res = diesel::select(("(1,2,3)".into_sql::<Text>().is_json())).get_result::<bool>(conn)?;
190
+ /// assert_eq!(res, false);
191
+ /// #
192
+ /// # Ok(())
193
+ /// # }
194
+ /// ```
195
+ #[ allow( clippy:: wrong_self_convention) ] // This is named after the sql operator
196
+ fn is_json ( self ) -> dsl:: IsJson < Self > {
197
+ IsJson :: new ( self )
198
+ }
199
+
200
+ /// Creates a PostgreSQL `IS NOT JSON` expression.
201
+ /// Requires PostgreSQL>=16
202
+ ///
203
+ /// This operator returns true whether an object is not a valid JSON
204
+ ///
205
+ /// # Example
206
+ ///
207
+ /// ```rust,no_run
208
+ /// # include!("../../doctest_setup.rs");
209
+ /// #
210
+ /// # fn main() {
211
+ /// # run_test().unwrap();
212
+ /// # }
213
+ /// #
214
+ /// # fn run_test() -> QueryResult<()> {
215
+ /// # use std::collections::Bound;
216
+ /// # use diesel::sql_types::Text;
217
+ /// #
218
+ /// # let conn = &mut establish_connection();
219
+ /// #
220
+ ///
221
+ /// let res = diesel::select(("1".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
222
+ /// assert_eq!(res, false);
223
+ /// let res = diesel::select(("[1,2,3]".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
224
+ /// assert_eq!(res, false);
225
+ /// let res = diesel::select(("{\"products\": [1,2,3]}".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
226
+ /// assert_eq!(res, false);
227
+ /// let res = diesel::select(("(1,2,3)".into_sql::<Text>().is_not_json())).get_result::<bool>(conn)?;
228
+ /// assert_eq!(res, true);
229
+ /// #
230
+ /// # Ok(())
231
+ /// # }
232
+ /// ```
233
+ #[ allow( clippy:: wrong_self_convention) ] // This is named after the sql operator
234
+ fn is_not_json ( self ) -> dsl:: IsNotJson < Self > {
235
+ IsNotJson :: new ( self )
236
+ }
161
237
}
162
238
163
239
impl < T : Expression > PgExpressionMethods for T { }
0 commit comments