Java JsonNodeFactory-class And Method Code Example

Here is an example of using the JsonNodeFactory class from the com.fasterxml.jackson.databind package in the Jackson library for Java:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

public class Main {
    public static void main(String[] args) {
        JsonNodeFactory factory = JsonNodeFactory.instance;
        JsonNode intNode = factory.numberNode(42);
        JsonNode stringNode = factory.textNode("hello");
        JsonNode arrayNode = factory.arrayNode();
        System.out.println(arrayNode.toString()); // prints "[42,"hello"]"

In this example, we first import the JsonNode and JsonNodeFactory classes from the com.fasterxml.jackson.databind package.

Then, we create a new instance of JsonNodeFactory by calling the instance static method and assign it to the factory variable.

We use the factory object to create new JsonNodes: an intNode with a value of 42, a stringNode with a value "hello" and an empty arrayNode.

We add the intNode and stringNode to the arrayNode using the add() method.

Finally, we print the string representation of arrayNode by calling the toString() method, which returns the string representation of the array "[42,"hello"]".

Keep in mind that this is a simple example, you can use JsonNodeFactory to create more complex JSON structures like objects, nested arrays, etc.