@@ -890,13 +890,25 @@ impl Json {
890
890
891
891
/// If the Json value is an Object, returns the value associated with the provided key.
892
892
/// Otherwise, returns None.
893
+ // NOTE(stage0): remove function after a snapshot
894
+ #[ cfg( stage0) ]
893
895
pub fn find < ' a > ( & ' a self , key : & str ) -> Option < & ' a Json > {
894
896
match self {
895
897
& Object ( ref map) => map. find_with ( |s| key. cmp ( & s. as_slice ( ) ) ) ,
896
898
_ => None
897
899
}
898
900
}
899
901
902
+ /// If the Json value is an Object, returns the value associated with the provided key.
903
+ /// Otherwise, returns None.
904
+ #[ cfg( not( stage0) ) ] // NOTE(stage0): remove cfg after a snapshot
905
+ pub fn find < ' a > ( & ' a self , key : & str ) -> Option < & ' a Json > {
906
+ match self {
907
+ & Object ( ref map) => map. find_with ( |s| key. cmp ( s. as_slice ( ) ) ) ,
908
+ _ => None
909
+ }
910
+ }
911
+
900
912
/// Attempts to get a nested Json Object for each key in `keys`.
901
913
/// If any key is found not to exist, find_path will return None.
902
914
/// Otherwise, it will return the Json value associated with the final key.
@@ -914,6 +926,8 @@ impl Json {
914
926
/// If the Json value is an Object, performs a depth-first search until
915
927
/// a value associated with the provided key is found. If no value is found
916
928
/// or the Json value is not an Object, returns None.
929
+ // NOTE(stage0): remove function after a snapshot
930
+ #[ cfg( stage0) ]
917
931
pub fn search < ' a > ( & ' a self , key : & str ) -> Option < & ' a Json > {
918
932
match self {
919
933
& Object ( ref map) => {
@@ -934,6 +948,30 @@ impl Json {
934
948
}
935
949
}
936
950
951
+ /// If the Json value is an Object, performs a depth-first search until
952
+ /// a value associated with the provided key is found. If no value is found
953
+ /// or the Json value is not an Object, returns None.
954
+ #[ cfg( not( stage0) ) ] // NOTE(stage0): remove cfg after a snapshot
955
+ pub fn search < ' a > ( & ' a self , key : & str ) -> Option < & ' a Json > {
956
+ match self {
957
+ & Object ( ref map) => {
958
+ match map. find_with ( |s| key. cmp ( s. as_slice ( ) ) ) {
959
+ Some ( json_value) => Some ( json_value) ,
960
+ None => {
961
+ for ( _, v) in map. iter ( ) {
962
+ match v. search ( key) {
963
+ x if x. is_some ( ) => return x,
964
+ _ => ( )
965
+ }
966
+ }
967
+ None
968
+ }
969
+ }
970
+ } ,
971
+ _ => None
972
+ }
973
+ }
974
+
937
975
/// Returns true if the Json value is an Object. Returns false otherwise.
938
976
pub fn is_object < ' a > ( & ' a self ) -> bool {
939
977
self . as_object ( ) . is_some ( )
0 commit comments