I have a simple class in java like below:
class Simple {
private String name;
private String email;
}
I want to have behaviour of java.util.List<Simple> and Simple both according to input data that my program receives.
i.e.
Case 1:: if my program receives below kind of json-array input
{"simple" : [ {"name":"a", "email" : "[email protected]"}, {"name":"b", "email" : "[email protected]"} ]}
I need to parse it using List<Simple>
Case 2:: if my program receives below kind of json-object input
{"simple" : {"name":"c", "email" : "[email protected]"} }
I need to parse it using Simple
Note: I have tried using JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, but the problem is it is basically converting single value also into json-array at the time of writing json.
I need to persist json as it is, is there any other way to achieve this?
To avoid any
Jacksoncustomisation I would create wrapper class with anObject simpleproperty. We can add two extra checking methods and two extra casting methods. It will allowJacksonto do it's logic and in runtime we can check what actually we have:Above code prints: