Created
June 24, 2024 06:56
-
-
Save PRASANTHRAJENDRAN/2ce21919abe0d1b8ce4e8115d8c2fd96 to your computer and use it in GitHub Desktop.
Perform Deep copy properties from one Java bean to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.fasterxml.jackson.core.type.TypeReference; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.google.gson.Gson; | |
| import java.util.Collections; | |
| import java.util.List; | |
| public class DeepCloneExample { | |
| public static void main(String[] args) throws Exception { | |
| SourceBean source = new SourceBean(); | |
| source.setName("John"); | |
| source.setAge(30); | |
| source.setAddress(new Address("123 Main St", "Springfield")); | |
| //USING OBJECTMAPPER | |
| ObjectMapper objectMapper = new ObjectMapper(); | |
| //Approach 1. Bean Deep copy logic | |
| TargetBean targetBean = objectMapper.convertValue(source, TargetBean.class); | |
| System.out.println("Target Bean: " + objectMapper.writeValueAsString(targetBean)); | |
| //Approach 2. List of Bean Deep copy logic using two-way approach | |
| List<SourceBean> sourceBeanList = Collections.singletonList(source); | |
| List<TargetBean> targetBeanList = objectMapper.convertValue(sourceBeanList, new TypeReference<List<TargetBean>>() { | |
| }); | |
| System.out.println("Target Bean List: " + objectMapper.writeValueAsString(targetBeanList)); | |
| //Approach 3. List of Bean Deep copy logic using two-way approach | |
| List<SourceBean> sourceBeanList1 = Collections.singletonList(source); | |
| String jsonString1 = objectMapper.writeValueAsString(sourceBeanList1); | |
| List<TargetBean> targetBeanList1 = objectMapper.readValue(jsonString1, new TypeReference<List<TargetBean>>() { | |
| }); | |
| System.out.println("Target Bean List: " + objectMapper.writeValueAsString(targetBeanList1)); | |
| //USING GSON | |
| Gson gson = new Gson(); | |
| String json = gson.toJson(source); | |
| TargetBean targetBeanGson = gson.fromJson(json, TargetBean.class); | |
| System.out.println("Target Bean: " + gson.toJson(targetBeanGson)); | |
| } | |
| } | |
| class SourceBean { | |
| private String name; | |
| private int age; | |
| private Address address; | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| public Address getAddress() { | |
| return address; | |
| } | |
| public void setAddress(Address address) { | |
| this.address = address; | |
| } | |
| } | |
| class TargetBean { | |
| private String name; | |
| private int age; | |
| private Address address; | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public void setAge(int age) { | |
| this.age = age; | |
| } | |
| public Address getAddress() { | |
| return address; | |
| } | |
| public void setAddress(Address address) { | |
| this.address = address; | |
| } | |
| } | |
| class Address { | |
| private String street; | |
| private String city; | |
| public Address() { | |
| } | |
| public Address(String street, String city) { | |
| this.street = street; | |
| this.city = city; | |
| } | |
| public String getStreet() { | |
| return street; | |
| } | |
| public void setStreet(String street) { | |
| this.street = street; | |
| } | |
| public String getCity() { | |
| return city; | |
| } | |
| public void setCity(String city) { | |
| this.city = city; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment