Object Cloning and its Mapping Using Object Mapper
Posted By : Pavan Kumar | 30-Aug-2019
Introduction: ObjectMapper is class of fasterxml library which can be use for multi purpose like convert JSON to Java Object or can be use Java Object to JSON. It is also use for serialize and deserialize the object. So in this blog I am going to tell you how you can copy properties from one Object of a class to another class object or can achive deep cloning.
FasterXMl: fasterxml is java based library which can be use multi purpose it give the no. of features it have to type of parser XML based which use to deserialize or serialize in the form of XML or another one is JSON based which is used for serialize and deserialize in the form of JSON. XML based parser is used FasterXML and JSON based parser use Jackson library classes.
So I have a Student class are as follows:
public Class Student{
private String name;
private int age;
private String college;
Student(String name,int age,String college){
this.name= name;
this.age = age;
this.college = college;
}
Student(){}//default constructor
}
Another is DTO class of Student
public Class StudentDTO{
private String name;
private int age;
private String college;
}
After that I Intialize the create a Object Student class and copy properties Using Object mapper rather then using constructor of DTO class.
public static final DateTimeFormatter FORMATTER = ofPattern("yyyy-MM-dd");
public void getStudentDTO(){
Student student = new Student("pradeep",25,"SRCEM");
StudentDTO studentDTO = copyPropertiesByMapper(student,StudentDTO.class);
}
public static <E extends Object,T extends Object> T copyPropertiesByMapper(E object,Class<T> valueType,String... fields){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JavaTimeModule javaTimeModule = new JavaTimeModule();
SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept(fields);
FilterProvider filters = new SimpleFilterProvider().addFilter("PropertyFilterMixIn", theFilter);
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(FORMATTER));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(FORMATTER));
objectMapper.registerModule(javaTimeModule);
try {
objectMapper.writer(filters);
String json = objectMapper.writeValueAsString(object);
return objectMapper.readValue(json, valueType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@JsonFilter("JSON_FILTER")
class PropertyFilterMixIn {}
Using This function you can also ignore properties like that:
public void getStudentDTO(){
Student student = new Student("pradeep",25,"SRCEM");
StudentDTO studentDTO = copyPropertiesByMapper(student,StudentDTO.class,"name"); //It will copy property except name property
}
And you can also copy properties of List like that:
public void getStudentDTO(){
List<Student> students = new ArrayList();
students.add(new Student("pradeep",25,"SRCEM"));
students.add(new Student("Rahul",25,"SRGOC"));
List<StudentDTO> studentDTOs = copyPropertiesOfListByMapper(students,StudentDTO.class);
}
public static <T extends Object,E extends Object> List<E> copyPropertiesOfListByMapper(List<T> objects, Class className) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(FORMATTER));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(FORMATTER));
mapper.registerModule(javaTimeModule);
try {
return mapper.readValue(mapper.writeValueAsString(objects), mapper.getTypeFactory().constructCollectionType(
List.class, className));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
If you want to clone the Object use this function like that.
public void getStudentDTO(){
List<Student> students = new ArrayList();
students.add(new Student("pradeep",25,"SRCEM"));
students.add(new Student("Rahul",25,"SRGOC"));
List<Student> copyOfStudents = copyPropertiesOfListByMapper(students,Student.class);
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pavan Kumar
Pavan is a bright Java developer. He is a learner by heart and has a passion and profile to adapt various technologies.