public boolean replaceObject ( JsonObject e1 , JsonObject e2 , String ... path ) { JsonElement compareElement = e1 . get ( path ) ; if ( compareElement == null ) { throw new IllegalArgumentException ( "specified path may not be null in object " + StringUtils . join ( path ) ) ; } int i = 0 ; for ( JsonElement e : this ) { if ( e . isObject ( ) ) { JsonElement fieldValue = e . asObject ( ) . get ( path ) ; if ( compareElement . equals ( fieldValue ) ) { set ( i , e2 ) ; return true ; } } i ++ ; } return false ; }
Convenient replace method that allows you to replace an object based on field equality for a specified field . Useful if you have an id field in your objects . Note the array may contain non objects as well or objects without the specified field . Those elements won t be replaced of course .