All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.leinad75.maven.plugin.json.util.JsonTreeWalker Maven / Gradle / Ivy

There is a newer version: 2.0.9
Show newest version
package io.github.leinad75.maven.plugin.json.util;

import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.util.function.BiConsumer;

import com.fasterxml.jackson.databind.JsonNode;
import java.util.function.Consumer;

public class JsonTreeWalker {

  public void walkTree(JsonNode root, Consumer consumer) {
    walker(null, root, consumer);
  }

  private void walker(String nodename, JsonNode node, Consumer consumer) {
    String nameToPrint = nodename != null ? nodename : "must_be_root";
    if (node.isObject()) {
      consumer.accept((ObjectNode) node);
      node.fields().forEachRemaining(e -> walker(e.getKey(), e.getValue(), consumer));
    } else if (node.isArray()) {
      node.elements().forEachRemaining(n -> walker("array item of '" + nameToPrint + "'", n, consumer));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy