Skip to content

Instantly share code, notes, and snippets.

@pianovwork
Last active June 8, 2016 16:50
Show Gist options
  • Save pianovwork/795c0105790edf3cfb1868b7595269bd to your computer and use it in GitHub Desktop.
Save pianovwork/795c0105790edf3cfb1868b7595269bd to your computer and use it in GitHub Desktop.
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