@@ -62,8 +62,52 @@ impl JsonValue {
6262 }
6363 }
6464
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+ pub fn is_empty ( & self ) -> bool {
101+ match * self {
102+ JsonValue :: String ( ref string) => string. is_empty ( ) ,
103+ JsonValue :: Number ( ref float) => !float. is_normal ( ) ,
104+ JsonValue :: Boolean ( ref boolean) => !boolean,
105+ JsonValue :: Null => true ,
106+ JsonValue :: Array ( ref vec) => vec. is_empty ( ) ,
107+ JsonValue :: Object ( ref btree) => btree. is_empty ( ) ,
108+ }
109+ }
110+
67111 #[ deprecated( since="0.6.1" , note="Use `as_str` instead" ) ]
68112 pub fn as_string ( & self ) -> JsonResult < & String > {
69113 match * self {
@@ -79,13 +123,6 @@ impl JsonValue {
79123 }
80124 }
81125
82- pub fn is_number ( & self ) -> bool {
83- match * self {
84- JsonValue :: Number ( _) => true ,
85- _ => false ,
86- }
87- }
88-
89126 #[ deprecated( since="0.6.1" , note="Use `as_f64` instead" ) ]
90127 pub fn as_number ( & self ) -> JsonResult < & f64 > {
91128 match * self {
@@ -145,10 +182,10 @@ impl JsonValue {
145182 self . as_f64 ( ) . and_then ( |value| f64_to_singed ! ( isize , value) )
146183 }
147184
148- pub fn is_boolean ( & self ) -> bool {
185+ pub fn as_bool ( & self ) -> Option < bool > {
149186 match * self {
150- JsonValue :: Boolean ( _ ) => true ,
151- _ => false
187+ JsonValue :: Boolean ( ref value ) => Some ( * value ) ,
188+ _ => None
152189 }
153190 }
154191
@@ -160,34 +197,6 @@ impl JsonValue {
160197 }
161198 }
162199
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-
191200 /// Works on `JsonValue::Object` - create or override key with value.
192201 #[ must_use]
193202 #[ deprecated( since="0.6.0" , note="Use `object[key] = value.into()` instead" ) ]
@@ -262,6 +271,17 @@ impl JsonValue {
262271 }
263272 }
264273
274+ /// Works on `JsonValue::Array` - remove and return last element from
275+ /// an array. On failure returns a null.
276+ pub fn pop ( & mut self ) -> JsonValue {
277+ match * self {
278+ JsonValue :: Array ( ref mut vec) => {
279+ vec. pop ( ) . unwrap_or ( JsonValue :: Null )
280+ } ,
281+ _ => JsonValue :: Null
282+ }
283+ }
284+
265285 /// Works on `JsonValue::Array` - gets a reference to a value at index.
266286 /// For most purposes consider using `array[index]` instead.
267287 #[ deprecated( since="0.6.0" , note="Use `array[index]` instead" ) ]
@@ -358,6 +378,29 @@ impl JsonValue {
358378 _ => EntriesMut :: None
359379 }
360380 }
381+
382+ /// Works on `JsonValue::Object` - remove a key and return the value it held.
383+ /// If the key was not present, the method is called on anything but an
384+ /// object, it will return a null.
385+ pub fn remove ( & mut self , key : & str ) -> JsonValue {
386+ match * self {
387+ JsonValue :: Object ( ref mut btree) => {
388+ btree. remove ( key) . unwrap_or ( JsonValue :: Null )
389+ } ,
390+ _ => JsonValue :: Null
391+ }
392+ }
393+
394+ /// When called on an array or an object, will wipe them clean. When called
395+ /// on a string will clear the string. Numbers and booleans become null.
396+ pub fn clear ( & mut self ) {
397+ match * self {
398+ JsonValue :: String ( ref mut string) => string. clear ( ) ,
399+ JsonValue :: Object ( ref mut btree) => btree. clear ( ) ,
400+ JsonValue :: Array ( ref mut vec) => vec. clear ( ) ,
401+ _ => * self = JsonValue :: Null ,
402+ }
403+ }
361404}
362405
363406/// Implements indexing by `usize` to easily access array members:
0 commit comments