Home > other >  Make POJO dynamic in nature to accept any amount of JSON array variables
Make POJO dynamic in nature to accept any amount of JSON array variables

Time:12-08

I have a Bean class which I have constructed according to the response I have got. Below is my Postman response.

{
    "EU": [
        {
            "calId": "EU",
            "calDate": "2022-11-01",
            "prevBusinessDay": "2022-11-01",
            "nextBusinessDay": "2022-11-01",
            "businessDay": true,
            "monthEndBusinessDay": false
        }
    ],
    "AU": [
        {
            "calId": "AU",
            "calDate": "2022-11-01",
            "prevBusinessDay": "2022-11-01",
            "nextBusinessDay": "2022-11-01",
            "businessDay": true,
            "monthEndBusinessDay": false
        }
    ]
}

According to this I have constructed the Bean as follows

@Data
@AllArgsConstructor
@NoArgsConstructor

public class IndexCalendarDateResponseBean {
    @JsonProperty("EU")
    private List<IndexCalendarDateResponseWrapper> EU;
    
    @JsonProperty("AU")
    private List<IndexCalendarDateResponseWrapper> AU;

}

IndexCalendarDateResponseWrapper is another POJO which contains all the variables calId,calDate etc which are inside the two JSON arrays.

My issue here is every time this array names cannot be EU and AU. This depends on whatever I pass in the API request URI path parameters. If I pass something else, then that will be the JSON array variable names. So my bean will not work for that. So I wanted to construct a bean that should be dynamic in nature. What I am doing with this bean is I am using this bean to fetch an API response as follows.

IndexCalendarDateResponseBean actualRIOutput = JsonPath.from(response.extract().asInputStream()).getObject("", IndexCalendarDateResponseBean.class);

So how to construct a bean such that even if I pass say something else as path parameters say JU, BU, and SU or JU and BU anything, it will fetch the response for me? Also the JSON array variables I am passing in path parameters can also vary in quantity i.e. it can be two, three or any number of paramaters I can pass. So that also the bean should accept? Is any other bean needed for this idea to support?

CodePudding user response:

So how to construct a bean such that even if I pass say something else as path parameters say JU, BU, and SU or JU and BU anything, it will fetch the response for me?

You can create a field of type Map<String, List<IndexCalendarDateResponseWrapper>> and make use of the method annotated with @JsonAnySetter to populate it.

If you need the ability to serialize this POJO into the same JSON, then you can add another method exposing the data of the Map annotated with @JsonAnyGetter.

public class IndexCalendarDateResponseBean {
    private Map<String, List<IndexCalendarDateResponseWrapper>> responseWrappers = new HashMap<>();
    
    @JsonAnySetter
    public void readResponseWrappers(String key, List<IndexCalendarDateResponseWrapper> value) {
        responseWrappers.put(key,value);
    }

    @JsonAnyGetter
    public Map<String, List<IndexCalendarDateResponseWrapper>> writeResponseWrappers() {
        return responseWrappers;
    }
}

Also the JSON array variables I am passing in path parameters can also vary in quantity i.e. it can be two, three or any number of paramaters

When you're deserializing JSON into a POJO missing properties would be set to default values of the corresponding type (for instance, null for reference types).

When you're serializing a POJO into JSON if you don't want to include null fields you can make use of the annotation @JsonInclude provide the value of JsonInclude.Include.NON_NULL or JsonInclude.Include.NON_EMPTY (depending on your requirements).

Example:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class IndexCalendarDateResponseWrapper {
    // field, constructors, etc.
}

CodePudding user response:

Why not replace IndexCalendarDateResponseBean with Map<Stirg,List< IndexCalendarDateResponseWrapper>>

  • Related