简体   繁体   中英

How to combine multiple thirdparty POJO into single POJO

We have multiple third party pojo which we wan to combine into single pojo and use that single pojo to map to a JSON using jackson.

third party pojo -

public class ThirdPartyPojo1 {

    private String random1

    //public setters and getters

}
public class ThirdPartyPojo2 {

    private String random2

    //public setters and getters

}

we wan to combine these to form a single pojo like -

public class ourPojo {
     private String random1;
     private String random2;

     //public setters and getters
}

we will use jackon to serialize this into a JSON string. How can we achieve this?

This is my way to solve your question. I am not sure if it has better solution so you may take this as a reference. I use ObjectReader readerForUpdating() to merge multiple json sources but this is limited to shallow copy. You may need another way to do with more complicated object. Here is my code:

package jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

public class Test {

    static ObjectMapper mapper = new ObjectMapper();
    
    public static void main(String[] args) throws Exception {
        // Create instance and convert it to json
        ThirdPartyPojo1 obj1 = new ThirdPartyPojo1();
        obj1.setRandom1("value 1");
        String json1 = toJson(obj1);
        
        // Create instance and convert it to json
        ThirdPartyPojo2 obj2 = new ThirdPartyPojo2();
        obj2.setRandom2("value 2");
        String json2 = toJson(obj2);
        
        // Suppose the field names of ThirdPartyPojo are corresponding to ourPojo
        // Firstly, use ObjectMapper readValue() to get ThirdPartyPojo1 field i.e. random1 
        ourPojo obj3 = mapper.readValue(json1, ourPojo.class);
        
        // Secondly, use ObjectReader to update ourPojo object
        // Notes that it makes shallow copy only
        ObjectReader updater = mapper.readerForUpdating(obj3);
        // Update ourPojo from ThirdPartyPojo2 field i.e. random2
        obj3 = updater.readValue(json2);
        
        // The result displays a merging json from your single POJO
        System.out.println(toJson(obj3));
    }
    
    static String toJson(Object obj) throws JsonProcessingException {
        return mapper.writeValueAsString(obj);
    }

}

class ThirdPartyPojo1 {
    private String random1;
    // public setters and getters
}

class ThirdPartyPojo2 {
    private String random2;
    // public setters and getters
}

class ourPojo {
    private String random1;
    private String random2;
    //public setters and getters
}

Maven:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.14.1</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM