eGoLai eGoLai - 3 months ago 8
Java Question

How to expand and object in JSON body?

I have the next classes to have the appropriate json bodies:

public class MyResponse {

private String meta;
private String info;

private String respOne;
//...
}


And

public class MyResponseNew {

private String meta;
private String info;

private String respNew;
private String respbBest;

//...
}


The JSONs I need:

{
"meta": "",
"info": "",

"respOne": ""
}


And

{
"meta": "",
"info": "",

"respNew": "",
"respbBest": ""
}


So, I want to extract general info to one class and have something like this:

public class GeneralSubResponse {
private String meta;
private String info;

//..

}

public class MyResponse {

/*@JsonExpanded*/
private GeneralSubResponse generalInfo;

private String respOne;

//...

}


String types - just for examples, there can be any object of any nesting...

Is it possible using Jackson lib? Or is there exist any other way to do something like that? The main concern is to not duplicate code for each Object.

Answer

You can do it with common class inheritance or adding it as unwrapped object:

Option 1: with Java inheritance:

public class MyResponse extends GeneralSubResponse {

    private String respOne;
    //...
}

public class MyResponseNew extends GeneralSubResponse{

    private String respNew;
    private String respbBest;

}

Option 2: JsonUnwrapped object as object property:

public class MyResponseUnwrapped{

    @JsonUnwrapped
    private GeneralSubResponse subResponse;

    private String respOne;
}

public class MyResponseNewUnwrapped{

    @JsonUnwrapped
    private GeneralSubResponse subResponse;

    private String respNew;
    private String respbBest;
}

Test (both options):

public class Test {

    public static String getJsonString(Object o){
        ObjectMapper mapper = new ObjectMapper();

        //For testing
        try {
            //Convert object to JSON string
            String jsonInString = mapper.writeValueAsString(o);
            //System.out.println(jsonInString);
            return jsonInString;


        } catch (JsonProcessingException e){
            e.printStackTrace();
        } 
        return null;
    }

    public static void main(String args[]){
        MyResponse myResponse = new MyResponse();
        myResponse.setInfo("info");
        myResponse.setMeta("meta");
        myResponse.setRespOne("respOne");
        System.out.println(myResponse.getClass().getSimpleName() + " = " + Test.getJsonString(myResponse ));
        System.out.println("------------------------------");
        MyResponseNew myResponseNew = new MyResponseNew();
        myResponseNew.setInfo("infoNew");
        myResponseNew.setMeta("metaNew");
        myResponseNew.setRespbBest("respBest");
        myResponseNew.setRespNew("respNew");
        System.out.println(myResponseNew.getClass().getSimpleName() + " = " + Test.getJsonString(myResponseNew));
        System.out.println("------------------------------");
        MyResponseUnwrapped myResponseUnwrapped = new MyResponseUnwrapped();
        GeneralSubResponse subResponse = new GeneralSubResponse();
        subResponse.setInfo("infoUnwrapped");
        subResponse.setMeta("metaUnwrapped");
        myResponseUnwrapped.setSubResponse(subResponse );
        myResponseUnwrapped.setRespOne("respTwo");
        System.out.println(myResponseUnwrapped.getClass().getSimpleName() + " = " + Test.getJsonString(myResponseUnwrapped));
        System.out.println("------------------------------");
        MyResponseNewUnwrapped myResponseNewUnwrapped = new MyResponseNewUnwrapped();
        GeneralSubResponse subResponse2 = new GeneralSubResponse();
        subResponse2.setInfo("infoNewUnwrapped");
        subResponse2.setMeta("metaNewUnwrapped");
        myResponseNewUnwrapped.setSubResponse(subResponse2 );
        myResponseNewUnwrapped.setRespbBest("respBestUnwrapped");
        myResponseNewUnwrapped.setRespNew("respNewUnwrapped");
        System.out.println(myResponseNewUnwrapped.getClass().getSimpleName() + " = " + Test.getJsonString(myResponseNewUnwrapped));
    }

}

Result:

MyResponse = {"meta":"meta","info":"info","respOne":"respOne"}
------------------------------
MyResponseNew = {"meta":"metaNew","info":"infoNew","respNew":"respNew","respbBest":"respBest"}
------------------------------
MyResponseUnwrapped = {"meta":"metaUnwrapped","info":"infoUnwrapped","respOne":"respTwo"}
------------------------------
MyResponseNewUnwrapped = {"meta":"metaNewUnwrapped","info":"infoNewUnwrapped","respNew":"respNewUnwrapped","respbBest":"respBestUnwrapped"}