File tree Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Expand file tree Collapse file tree 2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -277,6 +277,60 @@ impl JsonValue {
277
277
}
278
278
}
279
279
280
+ pub fn as_object ( & self ) -> Option < & Object > {
281
+ if let Self :: Object ( object) = self {
282
+ Some ( object)
283
+ } else {
284
+ None
285
+ }
286
+ }
287
+
288
+ pub fn as_object_mut ( & mut self ) -> Option < & mut Object > {
289
+ if let Self :: Object ( object) = self {
290
+ Some ( object)
291
+ } else {
292
+ None
293
+ }
294
+ }
295
+
296
+ pub fn as_array ( & self ) -> Option < & Vec < JsonValue > > {
297
+ if let Self :: Array ( array) = self {
298
+ Some ( array)
299
+ } else {
300
+ None
301
+ }
302
+ }
303
+
304
+ pub fn as_array_mut ( & mut self ) -> Option < & mut Vec < JsonValue > > {
305
+ if let Self :: Array ( array) = self {
306
+ Some ( array)
307
+ } else {
308
+ None
309
+ }
310
+ }
311
+
312
+ /// If `self` is a `JsonValue::Object`, then this looks up an entry by its `key` in the object.
313
+ ///
314
+ /// If `self` is not a `JsonValue::Object`, then this method returns `None`.
315
+ pub fn get ( & self , key : & str ) -> Option < & JsonValue > {
316
+ if let Self :: Object ( object) = self {
317
+ object. get ( key)
318
+ } else {
319
+ None
320
+ }
321
+ }
322
+
323
+ /// If `self` is a `JsonValue::Object`, then this looks up an entry by its `key` in the object.
324
+ ///
325
+ /// If `self` is not a `JsonValue::Object`, then this method returns `None`.
326
+ pub fn get_mut ( & mut self , key : & str ) -> Option < & mut JsonValue > {
327
+ if let Self :: Object ( object) = self {
328
+ object. get_mut ( key)
329
+ } else {
330
+ None
331
+ }
332
+ }
333
+
280
334
/// Obtain an integer at a fixed decimal point. This is useful for
281
335
/// converting monetary values and doing arithmetic on them without
282
336
/// rounding errors introduced by floating point operations.
Original file line number Diff line number Diff line change @@ -667,3 +667,38 @@ fn equality() {
667
667
assert_ne ! ( left, change_string) ;
668
668
assert_ne ! ( left, change_short) ;
669
669
}
670
+
671
+ #[ test]
672
+ fn as_object ( ) {
673
+ let obj = object ! { foo: [ "bar" ] } ;
674
+ assert ! ( obj. as_object( ) . is_some( ) ) ;
675
+ assert_eq ! ( * obj. as_object( ) . unwrap( ) . get( "foo" ) . unwrap( ) , array!{ "bar" } ) ;
676
+
677
+ assert ! ( ( array!{ } ) . as_object( ) . is_none( ) ) ;
678
+ assert ! ( JsonValue :: from( "string" ) . as_object( ) . is_none( ) ) ;
679
+ }
680
+
681
+ #[ test]
682
+ fn as_object_mut ( ) {
683
+ let mut obj = object ! { } ;
684
+ assert ! ( obj. as_object_mut( ) . is_some( ) ) ;
685
+ obj. as_object_mut ( ) . unwrap ( ) . insert ( "foo" , array ! { 42 } ) ;
686
+ assert_eq ! ( obj, object!{ foo: [ 42 ] } ) ;
687
+
688
+ assert ! ( ( array!{ } ) . as_object_mut( ) . is_none( ) ) ;
689
+ assert ! ( JsonValue :: from( "string" ) . as_object_mut( ) . is_none( ) ) ;
690
+ }
691
+
692
+ #[ test]
693
+ fn value_get ( ) {
694
+ let obj: JsonValue = object ! { foo: 42 } ;
695
+ assert_eq ! ( obj. get( "foo" ) , Some ( & JsonValue :: from( 42 ) ) ) ;
696
+ assert_eq ! ( obj. get( "missing" ) , None ) ;
697
+ }
698
+
699
+ #[ test]
700
+ fn value_get_mut ( ) {
701
+ let mut obj: JsonValue = object ! { foo: [ 42 ] } ;
702
+ obj. get_mut ( "foo" ) . unwrap ( ) . push ( 43 ) . unwrap ( ) ;
703
+ assert_eq ! ( obj, object!{ foo: [ 42 , 43 ] } ) ;
704
+ }
You can’t perform that action at this time.
0 commit comments