Skip to main content

Posts

Showing posts with the label jackson parser

How to convert java object to Map or Properties

Sometimes, we may need to convert the java objet to key-value pairs, for example Map or Properties class. It can be doable in multiple ways like using reflection or jackson library. We are going to use jackson library to achieve it here. Maven dependency <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> Employee.java We will use this class to convert to map and properties class. class Employee{ private long id; private String name; private Integer age; public Employee(long id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } //getter methods //setter methods } Generic method to convert the object Jackson library provides ObjectMapper class which provides "convertValue" method to convert the java objects. Below is the co...