Tree Model Nodes in Jackson
In Jackson, Tree Model Node is one of the most important concepts which we are going to discuss in this section. We will use Tree Model Node for various conversions and for adding, modifying, and removing nodes.
Let's understand how we can create a Node, transform a Node, manipulate a Node, locate a Node, and iterate over the Nodes one by one:
Creating a Node
First, we will create an instance of the ObjectMapper class with the help of the default constructor.
ObjectMapper mapper =new ObjectMapper();
Now, we can construct a JsonNode from any one of the following three ways:
Let's take an example to understand how we can create a JsonNode by using the valueToTree() method.
Transforming a Node
We can transform a Tree Node into a string or Java object.
Convert Tree Node into a JSON String
We transform a Tree Node into a JSON string when we need to write a Tree Node as JSON to the destination. Here, the destination can be an OutputStream, a File, or a writer. We use the writeValue() method of the mapper class in the following way:
Let's take an example to understand how we can use the writeValue() method to write JsonNode as JSON into the destination file.
Convert Tree Node into a Java object
We can also transform JSON Node into a Java Object by using the treeToValue API. The treeToValue() method of the ObjectMapper class accept two parameters, i.e., JsonNode and Object.class:
The treeToValue() method is similar to the convertValue() method of ObjectMapper class.
We can also convert JsonNode to Java Object by using the token stream. We use the treeAsTokens() and readValue() methods in the following way:
Let's take an example in which we will convert a JsonNode into a Java object by using all these three ways:
Manipulating Tree Nodes
We can manipulate Tree Nodes by using Jackson. Manipulating Tree Nodes include Locating a Node, Adding a New Node, Editing a Node, and Removing a Node.
Let's understand each one of them one by one with an example:
Locating a specific Node
We use the following steps to locate a specific JsonNode from JSON:
Let's implement the code for locating a specific node by using above discussed steps:
Adding a New Node
We can add a new node as a child node of another node. We use the put() method for adding a node. We use put() method in the following way:
Jackson provides several overloaded variants of the put method, which we can use for adding new nodes of different value types.
However, Jackson provide several other methods for adding child node such as putArray(), putObject(), putPOJO(), putRawValue() and putNull().
Let's take an example to understand how we can use put() method to add a node as a child.
Editing a Node
Just like adding a new JsonNode, we can edit the value of a specific JsonNode. We use the following steps for editing a specific JsonNode:
Let's implement the code for editing a specific node by using above discussed steps:
Removing a Node
Just like adding or editing a node, we can also remove a specific node from the JsonNode. We remove a specific node by calling the remove() method or remove API on its parent node. We use the following steps for editing a specific JsonNode:
Let's implement the code for removing a specific node by using above discussed steps:
Iterating over the Nodes
In order to iterate over the nodes, we first need to reformat them into YAML. A JSON document can contain three types of nodes, i.e., Value, Object, and Array. JSON document contains nodes in a hierarchical tree structure. So, we will start iterating the JSON document at the top and go through all the child nodes.
We will use a recursive process for iterating the JSON nodes. In the recursive method, we will pass the root node of the JSON document.
We will use the following steps for iterating over the nodes:
Let's implement the code for iterating all the nodes by using the above-discussed steps:
Next TopicTypes of events in Java