@@ -62,8 +62,60 @@ impl JsonValue {
62
62
}
63
63
}
64
64
65
- /// Deprecated because the return type is planned to change to
66
- /// `Option<String>` eventually down the road.
65
+ pub fn is_number ( & self ) -> bool {
66
+ match * self {
67
+ JsonValue :: Number ( _) => true ,
68
+ _ => false ,
69
+ }
70
+ }
71
+
72
+ pub fn is_boolean ( & self ) -> bool {
73
+ match * self {
74
+ JsonValue :: Boolean ( _) => true ,
75
+ _ => false
76
+ }
77
+ }
78
+
79
+ pub fn is_null ( & self ) -> bool {
80
+ match * self {
81
+ JsonValue :: Null => true ,
82
+ _ => false ,
83
+ }
84
+ }
85
+
86
+ pub fn is_object ( & self ) -> bool {
87
+ match * self {
88
+ JsonValue :: Object ( _) => true ,
89
+ _ => false ,
90
+ }
91
+ }
92
+
93
+ pub fn is_array ( & self ) -> bool {
94
+ match * self {
95
+ JsonValue :: Array ( _) => true ,
96
+ _ => false ,
97
+ }
98
+ }
99
+
100
+ /// Checks whether the value is empty. Returns true for:
101
+ ///
102
+ /// - empty string (`""`)
103
+ /// - number `0`
104
+ /// - boolean `false`
105
+ /// - null
106
+ /// - empty array (`array![]`)
107
+ /// - empty object (`object!{}`)
108
+ pub fn is_empty ( & self ) -> bool {
109
+ match * self {
110
+ JsonValue :: String ( ref value) => value. is_empty ( ) ,
111
+ JsonValue :: Number ( ref value) => !value. is_normal ( ) ,
112
+ JsonValue :: Boolean ( ref value) => !value,
113
+ JsonValue :: Null => true ,
114
+ JsonValue :: Array ( ref value) => value. is_empty ( ) ,
115
+ JsonValue :: Object ( ref value) => value. is_empty ( ) ,
116
+ }
117
+ }
118
+
67
119
#[ deprecated( since="0.6.1" , note="Use `as_str` instead" ) ]
68
120
pub fn as_string ( & self ) -> JsonResult < & String > {
69
121
match * self {
@@ -79,13 +131,6 @@ impl JsonValue {
79
131
}
80
132
}
81
133
82
- pub fn is_number ( & self ) -> bool {
83
- match * self {
84
- JsonValue :: Number ( _) => true ,
85
- _ => false ,
86
- }
87
- }
88
-
89
134
#[ deprecated( since="0.6.1" , note="Use `as_f64` instead" ) ]
90
135
pub fn as_number ( & self ) -> JsonResult < & f64 > {
91
136
match * self {
@@ -145,10 +190,10 @@ impl JsonValue {
145
190
self . as_f64 ( ) . and_then ( |value| f64_to_singed ! ( isize , value) )
146
191
}
147
192
148
- pub fn is_boolean ( & self ) -> bool {
193
+ pub fn as_bool ( & self ) -> Option < bool > {
149
194
match * self {
150
- JsonValue :: Boolean ( _ ) => true ,
151
- _ => false
195
+ JsonValue :: Boolean ( ref value ) => Some ( * value ) ,
196
+ _ => None
152
197
}
153
198
}
154
199
@@ -160,34 +205,6 @@ impl JsonValue {
160
205
}
161
206
}
162
207
163
- pub fn as_bool ( & self ) -> Option < bool > {
164
- match * self {
165
- JsonValue :: Boolean ( ref value) => Some ( * value) ,
166
- _ => None
167
- }
168
- }
169
-
170
- pub fn is_null ( & self ) -> bool {
171
- match * self {
172
- JsonValue :: Null => true ,
173
- _ => false ,
174
- }
175
- }
176
-
177
- pub fn is_object ( & self ) -> bool {
178
- match * self {
179
- JsonValue :: Object ( _) => true ,
180
- _ => false ,
181
- }
182
- }
183
-
184
- pub fn is_array ( & self ) -> bool {
185
- match * self {
186
- JsonValue :: Array ( _) => true ,
187
- _ => false ,
188
- }
189
- }
190
-
191
208
/// Works on `JsonValue::Object` - create or override key with value.
192
209
#[ must_use]
193
210
#[ deprecated( since="0.6.0" , note="Use `object[key] = value.into()` instead" ) ]
@@ -262,6 +279,17 @@ impl JsonValue {
262
279
}
263
280
}
264
281
282
+ /// Works on `JsonValue::Array` - remove and return last element from
283
+ /// an array. On failure returns a null.
284
+ pub fn pop ( & mut self ) -> JsonValue {
285
+ match * self {
286
+ JsonValue :: Array ( ref mut vec) => {
287
+ vec. pop ( ) . unwrap_or ( JsonValue :: Null )
288
+ } ,
289
+ _ => JsonValue :: Null
290
+ }
291
+ }
292
+
265
293
/// Works on `JsonValue::Array` - gets a reference to a value at index.
266
294
/// For most purposes consider using `array[index]` instead.
267
295
#[ deprecated( since="0.6.0" , note="Use `array[index]` instead" ) ]
@@ -358,6 +386,29 @@ impl JsonValue {
358
386
_ => EntriesMut :: None
359
387
}
360
388
}
389
+
390
+ /// Works on `JsonValue::Object` - remove a key and return the value it held.
391
+ /// If the key was not present, the method is called on anything but an
392
+ /// object, it will return a null.
393
+ pub fn remove ( & mut self , key : & str ) -> JsonValue {
394
+ match * self {
395
+ JsonValue :: Object ( ref mut btree) => {
396
+ btree. remove ( key) . unwrap_or ( JsonValue :: Null )
397
+ } ,
398
+ _ => JsonValue :: Null
399
+ }
400
+ }
401
+
402
+ /// When called on an array or an object, will wipe them clean. When called
403
+ /// on a string will clear the string. Numbers and booleans become null.
404
+ pub fn clear ( & mut self ) {
405
+ match * self {
406
+ JsonValue :: String ( ref mut string) => string. clear ( ) ,
407
+ JsonValue :: Object ( ref mut btree) => btree. clear ( ) ,
408
+ JsonValue :: Array ( ref mut vec) => vec. clear ( ) ,
409
+ _ => * self = JsonValue :: Null ,
410
+ }
411
+ }
361
412
}
362
413
363
414
/// Implements indexing by `usize` to easily access array members:
@@ -411,7 +462,7 @@ impl IndexMut<usize> for JsonValue {
411
462
_ => {
412
463
* self = JsonValue :: new_array ( ) ;
413
464
self . push ( JsonValue :: Null ) . unwrap ( ) ;
414
- & mut self [ 0 ]
465
+ self . index_mut ( index )
415
466
}
416
467
}
417
468
}
@@ -470,7 +521,7 @@ impl<'a> IndexMut<&'a str> for JsonValue {
470
521
} ,
471
522
_ => {
472
523
* self = JsonValue :: new_object ( ) ;
473
- & mut self [ index]
524
+ self . index_mut ( index)
474
525
}
475
526
}
476
527
}
0 commit comments