I am calling an API which returns an object if there is a single item and a list of 0 or 2+ elements. I'd like to deserialize all the responses into a list.
I'd like to write a dynamic Deserializer or TypeAdapter which I can just annotate any field which might be a list or object. I am able to write the TypeAdapter for each class, but I can't figure out how to make it dynamic so that I can use the same class for all the keys.
sample inputs:
{ "fruits": [], "vegetables": { "name": "carrot" } }
{ "fruits": { "type": "apple" }, "vegetables": [ { "name": "carrot" }, { "name": "celery" } ] }
{ "fruits": [ { "type": "apple" }, { "type": "banana" } ], "vegetables": [] }
Desired POJOs:
public class FoodResponse {
@SomeAnnotation
private List<Fruit> fruits;
@SomeAnnotation
private List<Vegetable> vegetables;
}
public class Fruit {
private String type;
}
public class Vegetable {
private String name;
}
Do you have any idea how I might write something that can be this dynamic?