I'm facing a problem related to parsing json which have mixed arrays of child classes, and I need to provide that as java list back to client.
Sample JSON:
{
"status": "OK",
"results": [
{
"type": "one",
"Id": "2170676",
"count": "456",
"title": "title",
"description": "description",
"oneMemberOne": "11",
"oneMemberTwo": "12",
}
{
"type": "two",
"Id": "2170677",
"count": "123",
"title": "title",
"description": "description",
"twoMemberOne": "21",
"twoMemberTwo": "22",
}
]
}
I created one Parent class and two child class from this:
Num : type, Id, count, title, description fields
One extends Num : oneMemberOne, oneMemberTwo
Two extends Num : twoMemberOne, twoMemberTwo
Now my question:
I have a method for asking results. Say it List<Num> getResults()
I parse the data properly like this:
List<Num> result = new ArrayList<Num>(); JsonParser parser = new JsonParser(); JsonObject jsonObject = parser.parse(lastResponsePayload).getAsJsonObject() JsonArray results = jsonObject.get("results").getAsJsonArray(); for (JsonElement element : results) { JsonObject trs = element.getAsJsonObject(); String type = trs.get("type").getAsString(); if (type.equals("one") { One one = new Gson().fromJson(element, One.class); result.add(product); } else if (type.equals("two") { Two two = new Gson().fromJson(element, Two.class); result.add(metaproduct); } return result;
Now, on client side, after I get the list, i have to do this:
List<Num> results = getResults(); List<One> oness = new ArrayList<One>(); List<Two> twoss = new ArrayList<Two>(); for(Num n : results) { if(n.type.equals("one") oness.add((One) n); else twoss.add((Two) n);
Is this a good design for this scenario ?
User of this API has to downcast everytime based on the type field of parent class. Because webservice gives me mixed array of child classes, I have to do this. Is there any better approach to this problem ?
- One another approach in my mind is to create a
Result
class which contains two membersOne
andTwo
and provide my aList<Result>
instead ofList<Num>
, but then user has to check whether member isnull
or not and then take appropriate steps.
Thank you in advance.