Last active
June 8, 2016 16:50
-
-
Save pianovwork/795c0105790edf3cfb1868b7595269bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsonNodeHelper { | |
/** | |
* remove recursively all blank nodes (null, missed, array/object with size 0 and blank text) | |
* | |
* @param node target node where need to remove blank nodes | |
*/ | |
public static void removeBlankNodes(JsonNode node) { | |
if (node == null) { | |
return; | |
} | |
if (node.isContainerNode()) { | |
Iterator<JsonNode> iterator = node.iterator(); | |
while (iterator.hasNext()) { | |
JsonNode next = iterator.next(); | |
removeBlankNodes(next); | |
if (isBlankNode(next)) { | |
iterator.remove(); | |
} | |
} | |
} | |
} | |
public static boolean isBlankNode(JsonNode node) { | |
return node == null | |
|| node.isNull() | |
|| node.isMissingNode() | |
|| (node.isContainerNode() && node.size() == 0) | |
|| (node.isTextual() && StringUtils.isEmpty(node.textValue())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment