Skip to content

Commit

Permalink
Merge pull request #215 from milosimpson/removeRecursive
Browse files Browse the repository at this point in the history
Be more performant in the removeRecursive methods.
  • Loading branch information
Milo Simpson committed Jun 9, 2016
2 parents 0704ef5 + ef2b2de commit dcf544e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import java.util.List;
import java.util.Map;


/**
* Handy utilities that do NOT depend on JsonUtil lives here!
* Handy utilities that do NOT depend on JsonUtil / Jackson live here
*/
public class JoltUtils {

Expand All @@ -44,13 +43,14 @@ public static void removeRecursive( Object json, String keyToRemove ) {
Map<String, Object> jsonMap = cast(json);

// If this level of the tree has the key we are looking for, remove it
// Do the lookup instead of just the remove to avoid un-necessarily
// dying on ImmutableMaps.
if ( jsonMap.containsKey( keyToRemove ) ) {
jsonMap.remove( keyToRemove );
}

// regardless, recurse down the tree
for ( String subKey : jsonMap.keySet() ) {
Object value = jsonMap.get( subKey );
for ( Object value : jsonMap.values() ) {
removeRecursive( value, keyToRemove );
}
}
Expand Down
5 changes: 3 additions & 2 deletions json-utils/src/main/java/com/bazaarvoice/jolt/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ public static void removeRecursive( Object json, String keyToRemove ) {
Map<String, Object> jsonMap = (Map<String, Object>) json;

// If this level of the tree has the key we are looking for, remove it
// Do the lookup instead of just the remove to avoid un-necessarily
// dying on ImmutableMaps.
if ( jsonMap.containsKey( keyToRemove ) ) {
jsonMap.remove( keyToRemove );
}

// regardless, recurse down the tree
for ( String subKey : jsonMap.keySet() ) {
Object value = jsonMap.get( subKey );
for ( Object value : jsonMap.values() ) {
removeRecursive( value, keyToRemove );
}
}
Expand Down

0 comments on commit dcf544e

Please sign in to comment.