@@ -227,6 +227,130 @@ impl RawValue {
227
227
pub fn get ( & self ) -> & str {
228
228
& self . json
229
229
}
230
+
231
+ /// Returns the kind of JSON value contained within.
232
+ pub fn kind ( & self ) -> RawKind {
233
+ let first = self
234
+ . json
235
+ . as_bytes ( )
236
+ . first ( )
237
+ . expect ( "The `str` contained in `RawValue` can`t be an empty" ) ;
238
+
239
+ // `RawValue` has whitespace stripped so we know the first char is non-whitespace.
240
+ // `RawValue` is also valid JSON so the only possible set of chars that can come next
241
+ // are `[0-9]` or `[-pub fnt"[{]`.
242
+ match * first {
243
+ b'n' => RawKind :: Null ,
244
+ b't' | b'f' => RawKind :: Bool ,
245
+ b'"' => RawKind :: String ,
246
+ b'[' => RawKind :: Array ,
247
+ b'{' => RawKind :: Object ,
248
+ // A `RawValue` is already known to be valid, the only other possibility for
249
+ // valid JSON is that this is a number
250
+ _ => RawKind :: Number ,
251
+ }
252
+ }
253
+
254
+ /// Returns true if the `Value` is a Null. Returns false otherwise.
255
+ ///
256
+ /// ```
257
+ /// # use serde_json::json;
258
+ /// #
259
+ /// let val= json!(null);
260
+ /// let val = serde_json::value::to_raw_value(&val).unwrap();
261
+ ///
262
+ /// assert!(val.is_null());
263
+ /// ```
264
+ pub fn is_null ( & self ) -> bool {
265
+ matches ! ( self . kind( ) , RawKind :: Null )
266
+ }
267
+
268
+ /// Returns true if the `Value` is a Boolean. Returns false otherwise.
269
+ ///
270
+ /// ```
271
+ /// # use serde_json::json;
272
+ /// #
273
+ /// let val = json!(true);
274
+ /// let val = serde_json::value::to_raw_value(&val).unwrap();
275
+ ///
276
+ /// assert!(val.is_boolean());
277
+ /// ```
278
+ pub fn is_boolean ( & self ) -> bool {
279
+ matches ! ( self . kind( ) , RawKind :: Bool )
280
+ }
281
+
282
+ /// Returns true if the `Value` is a Number. Returns false otherwise.
283
+ ///
284
+ /// ```
285
+ /// # use serde_json::json;
286
+ /// #
287
+ /// let val = json!(101);
288
+ /// let val = serde_json::value::to_raw_value(&val).unwrap();
289
+ ///
290
+ /// assert!(val.is_number());
291
+ /// ```
292
+ pub fn is_number ( & self ) -> bool {
293
+ matches ! ( self . kind( ) , RawKind :: Number )
294
+ }
295
+
296
+ /// Returns true if the `Value` is a String. Returns false otherwise.
297
+ ///
298
+ /// ```
299
+ /// # use serde_json::json;
300
+ /// #
301
+ /// let val = json!("some string");
302
+ /// let val = serde_json::value::to_raw_value(&val).unwrap();
303
+ ///
304
+ /// assert!(val.is_string());
305
+ /// ```
306
+ pub fn is_string ( & self ) -> bool {
307
+ matches ! ( self . kind( ) , RawKind :: String )
308
+ }
309
+
310
+ /// Returns true if the `Value` is an Array. Returns false otherwise.
311
+ ///
312
+ /// ```
313
+ /// # use serde_json::json;
314
+ /// #
315
+ /// let val = json!(["an", "array"]);
316
+ /// let val = serde_json::value::to_raw_value(&val).unwrap();
317
+ ///
318
+ /// assert!(val.is_array());
319
+ /// ```
320
+ pub fn is_array ( & self ) -> bool {
321
+ matches ! ( self . kind( ) , RawKind :: Array )
322
+ }
323
+
324
+ /// Returns true if the `Value` is an Object. Returns false otherwise.
325
+ ///
326
+ /// ```
327
+ /// # use serde_json::json;
328
+ /// #
329
+ /// let val = json!({ "a": { "nested": true }, "b": ["an", "array"] });
330
+ /// let val = serde_json::value::to_raw_value(&val).unwrap();
331
+ ///
332
+ /// assert!(val.is_object());
333
+ /// ```
334
+ pub fn is_object ( & self ) -> bool {
335
+ matches ! ( self . kind( ) , RawKind :: Object )
336
+ }
337
+ }
338
+
339
+ // A kind of JSON value the `RawValue` contains.
340
+ #[ derive( Debug , Copy , Clone ) ]
341
+ pub enum RawKind {
342
+ /// Represents a JSON null value.
343
+ Null ,
344
+ /// Represents a JSON boolean.
345
+ Bool ,
346
+ /// Represents a JSON number, whether integer or floating point.
347
+ Number ,
348
+ /// Represents a JSON string.
349
+ String ,
350
+ /// Represents a JSON array.
351
+ Array ,
352
+ /// Represents a JSON object.
353
+ Object ,
230
354
}
231
355
232
356
impl From < Box < RawValue > > for Box < str > {
0 commit comments