Jackson Tree Model: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 48: Line 48:
public static void traverse(int depth, boolean indent, JsonNode node) {
public static void traverse(int depth, boolean indent, JsonNode node) {


        JsonNodeType type = node.getNodeType();
    JsonNodeType type = node.getNodeType();


        if (JsonNodeType.OBJECT.equals(type)) {
    if (JsonNodeType.OBJECT.equals(type)) {


            System.out.println("\n" + indentation(depth) + "{");
        System.out.println("\n" + indentation(depth) + "{");


            for(Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext(); ) {
        for(Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext(); ) {


                Map.Entry<String, JsonNode> field = i.next();
            Map.Entry<String, JsonNode> field = i.next();
                String fieldName = field.getKey();
            String fieldName = field.getKey();
                JsonNode value = field.getValue();
            JsonNode value = field.getValue();


                System.out.print(indentation(depth + 1) + "\"" + fieldName + "\": ");
            System.out.print(indentation(depth + 1) + "\"" + fieldName + "\": ");


                traverse(depth + 1, false, value);
            traverse(depth + 1, false, value);


                if (i.hasNext()) {
            if (i.hasNext()) {
 
                System.out.print(",");
                    System.out.print(",");
                }
 
                System.out.println();
             }
             }
 
             System.out.println();
             System.out.print(indentation(depth) + "}");
         }
         }
         else if (JsonNodeType.ARRAY.equals(type)) {
         System.out.print(indentation(depth) + "}");
 
    }
            System.out.println("\n" + indentation(depth) + "[");
    else if (JsonNodeType.ARRAY.equals(type)) {
 
            for(Iterator<JsonNode> i = node.iterator(); i.hasNext(); ) {
 
                traverse(depth + 1, true, i.next());


                if (i.hasNext()) {
        System.out.println("\n" + indentation(depth) + "[");


                    System.out.print(",");
        for(Iterator<JsonNode> i = node.iterator(); i.hasNext(); ) {
                }


                System.out.println();
          traverse(depth + 1, true, i.next());
            }


            System.out.print(indentation(depth) + "]");
          if (i.hasNext()) {
        }
              System.out.print(",");
        else {
          }
 
          System.out.println();
            displayLeaf(depth, indent, node);
      }
        }
      System.out.print(indentation(depth) + "]");
 
    }
        if (depth == 0) {
    else {
 
        displayLeaf(depth, indent, node);
            System.out.println();
    }
        }
}
}


public static void displayLeaf(int depth, boolean indent, JsonNode node) {
public static void displayLeaf(int depth, boolean indent, JsonNode node) {


        JsonNodeType type = node.getNodeType();
    JsonNodeType type = node.getNodeType();


        if (JsonNodeType.OBJECT.equals(type) || JsonNodeType.ARRAY.equals(type)) {
    if (JsonNodeType.OBJECT.equals(type) || JsonNodeType.ARRAY.equals(type)) {
        throw new IllegalArgumentException(node + " not a leaf");
    }


            throw new IllegalArgumentException(node + " not a leaf");
    if (indent) {
        }
        System.out.print(indentation(depth));
 
    }
        if (indent) {
 
            System.out.print(indentation(depth));
        }
 
        if (JsonNodeType.NULL.equals(type)) {
 
            System.out.print("null");
        }
        else if (JsonNodeType.BOOLEAN.equals(type)) {
 
            System.out.print(node.booleanValue());
        }
        else if (JsonNodeType.NUMBER.equals(type)) {
 
            System.out.print(node.numberValue());
        }
        else if (JsonNodeType.STRING.equals(type)) {


            System.out.print("\"" + node.textValue() + "\"");
    if (JsonNodeType.NULL.equals(type)) {
        }
        System.out.print("null");
        else {
    }
 
    else if (JsonNodeType.BOOLEAN.equals(type)) {
            throw new RuntimeException("NOT YET IMPLEMENTED");
        System.out.print(node.booleanValue());
        }
    }
    else if (JsonNodeType.NUMBER.equals(type)) {
        System.out.print(node.numberValue());
    }
    else if (JsonNodeType.STRING.equals(type)) {
        System.out.print("\"" + node.textValue() + "\"");
    }
    else {
        throw new RuntimeException("NOT YET IMPLEMENTED");
    }
}
}
</pre>
</pre>

Revision as of 23:54, 25 February 2017

Internal

Overview

This method converts a JSON document into a mutable in-memory tree representation of the JSON document. Tree model is arguably the most flexible of all three methods. The tree model is similar to the XML DOM.

JSON to Java with Tree Model

org.codehaus.jackson.map.ObjectMapper is the main API that builds trees from JSON content.

InputStream is = ...

ObjectMapper om = new ObjectMapper();

JsonNode root = om.readTree(is);

The trees consists of JsonNodes. Each node has one of the following types, coded as the JsonNodeType enum:

  • NULL
  • BOOLEAN
  • NUMBER
  • STRING
  • OBJECT
  • ARRAY
  • BINARY
  • POJO
  • MISSING

There is no "FIELD" node. If the node is an OBJECT, we get its fields with:

for(Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext(); ) {

    Map.Entry<String, JsonNode> field = i.next();
    String fieldName = field.getKey();
    JsonNode value = field.getValue();
    ...
}

This is a standard tree traversing method:

public static void traverse(int depth, boolean indent, JsonNode node) {

    JsonNodeType type = node.getNodeType();

    if (JsonNodeType.OBJECT.equals(type)) {

        System.out.println("\n" + indentation(depth) + "{");

        for(Iterator<Map.Entry<String, JsonNode>> i = node.fields(); i.hasNext(); ) {

            Map.Entry<String, JsonNode> field = i.next();
            String fieldName = field.getKey();
            JsonNode value = field.getValue();

            System.out.print(indentation(depth + 1) + "\"" + fieldName + "\": ");

            traverse(depth + 1, false, value);

            if (i.hasNext()) {
                System.out.print(",");
            }
            System.out.println();
        }
        System.out.print(indentation(depth) + "}");
    }
    else if (JsonNodeType.ARRAY.equals(type)) {

        System.out.println("\n" + indentation(depth) + "[");

        for(Iterator<JsonNode> i = node.iterator(); i.hasNext(); ) {

           traverse(depth + 1, true, i.next());

           if (i.hasNext()) {
               System.out.print(",");
           }
           System.out.println();
       }
       System.out.print(indentation(depth) + "]");
    }
    else {
        displayLeaf(depth, indent, node);
    }
}

public static void displayLeaf(int depth, boolean indent, JsonNode node) {

    JsonNodeType type = node.getNodeType();

    if (JsonNodeType.OBJECT.equals(type) || JsonNodeType.ARRAY.equals(type)) {
        throw new IllegalArgumentException(node + " not a leaf");
    }

    if (indent) {
        System.out.print(indentation(depth));
    }

    if (JsonNodeType.NULL.equals(type)) {
         System.out.print("null");
    }
    else if (JsonNodeType.BOOLEAN.equals(type)) {
        System.out.print(node.booleanValue());
    }
    else if (JsonNodeType.NUMBER.equals(type)) {
        System.out.print(node.numberValue());
    }
    else if (JsonNodeType.STRING.equals(type)) {
        System.out.print("\"" + node.textValue() + "\"");
    }
    else {
        throw new RuntimeException("NOT YET IMPLEMENTED");
    }
}

JSON to Java with Tree Model Code Example


Java to JSON with Tree Model