Java String Remove removeRecursive(Object json, String keyToRemove)

Here you can find the source of removeRecursive(Object json, String keyToRemove)

Description

Removes a key recursively from anywhere in a JSON document.

License

Apache License

Parameter

Parameter Description
json the Jackson Object version of the JSON document (contents changed by this call)
keyToRemove the key to remove from the document

Declaration

@Deprecated
public static void removeRecursive(Object json, String keyToRemove) 

Method Source Code

//package com.java2s;
/*/* w  ww .  ja v  a  2 s.co  m*/
 * Copyright 2013 Bazaarvoice, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;
import java.util.Map;

public class Main {
    /**
     * Removes a key recursively from anywhere in a JSON document.
     * NOTE: mutates its input.
     *
     * Deprecated: use JoltUtils instead
     *
     * @param json        the Jackson Object version of the JSON document
     *                    (contents changed by this call)
     * @param keyToRemove the key to remove from the document
     */
    @Deprecated
    public static void removeRecursive(Object json, String keyToRemove) {
        if ((json == null) || (keyToRemove == null)) {
            return;
        }
        if (json instanceof Map) {
            @SuppressWarnings("unchecked")
            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 (Object value : jsonMap.values()) {
                removeRecursive(value, keyToRemove);
            }
        }
        if (json instanceof List) {
            for (Object value : (List) json) {
                removeRecursive(value, keyToRemove);
            }
        }
    }
}

Related

  1. removePostfixedNewline(String s)
  2. removeProperty(String name)
  3. removePropertyNameModifier(String name)
  4. removePunctuation(String str)
  5. removePunctuation(String value)
  6. removeSomeOne(String words, int count)
  7. removeSpaces(String orig)
  8. removeSpecialChars(final String word)
  9. removeSpecialCharsForSQLRegExp(String _s)