I am using
below Jackson dependency.
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9.3</version> </dependency>
Below
snippet is used to convert an object to string.
public static String marshal(Object obj) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); }
Below
snippet is used to convert json string to an object.
public static <T> T unmarshal(Class<T> clazz, String json) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); return (T) mapper.readValue(json, clazz); }
Find the
utility class.
package com.sample.app.util; import java.io.IOException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonUtil { public static String marshal(Object obj) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(obj); } public static <T> T unmarshal(Class<T> clazz, String json) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); return (T) mapper.readValue(json, clazz); } }
Let’s test
the utility class with Employee class.
package com.sample.app.model; public class Address { private String city; private String state; private String country; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Address [city=").append(city).append(", state=").append(state).append(", country=") .append(country).append("]"); return builder.toString(); } }
Employee.java
package com.sample.app.model; public class Employee { private int id; private String firstName; private String lastName; private Address address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Employee [id=").append(id).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append(", address=").append(address).append("]"); return builder.toString(); } }
App.java
package com.sample.app; import java.io.IOException; import com.sample.app.model.Address; import com.sample.app.model.Employee; import com.sample.app.util.JacksonUtil; public class App { public static void main(String args[]) throws IOException { Employee emp = new Employee(); emp.setId(1); emp.setFirstName("krishna"); emp.setLastName("Majety"); Address addr = new Address(); addr.setCity("Chirala"); addr.setCountry("India"); addr.setState("Andhra Pradesh"); emp.setAddress(addr); String serialized = JacksonUtil.marshal(emp); Employee emp1 = JacksonUtil.unmarshal(Employee.class, serialized); System.out.println(serialized); System.out.println(emp1); } }
Run
App.java, you can see below messages in console.
{"id":1,"firstName":"krishna","lastName":"Majety","address":{"city":"Chirala","state":"Andhra Pradesh","country":"India"}} Employee [id=1, firstName=krishna, lastName=Majety, address=Address [city=Chirala, state=Andhra Pradesh, country=India]]
No comments:
Post a Comment