Java POJONode-class And Method Code Example


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.POJONode;

public class Example {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        Person person = new Person("John", "Doe");
        POJONode pojoNode = new POJONode(person);

        // pojoNode is an instance of POJONode
        System.out.println(pojoNode.getPojo()); // prints "Person{firstName='John', lastName='Doe'}"
    }
}

class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return "Person{firstName='" + firstName + "', lastName='" + lastName + "'}";
    }
}

This example shows how to create an instance of the POJONode class, which is a special type of JsonNode that wraps a plain old Java object (POJO). The POJONode class has a constructor that takes an instance of any Java object, which can be passed as a parameter to create a POJONode. Once obtained, you can get the POJO wrapped by the node using the getPojo() method.

Note that, POJONode allows to store any POJO within a JsonNode tree, but it doesn't serialize or deserialize the POJO to json, you need to use ObjectMapper to achieve that.