One of our servers has an API endpoint that sometimes returns MAC addresses in a list (up to 3) and sometimes it returns a single MAC address
Can be this:
{
"mac_addrs": [
"11:11:11:11:11:11",
"22:22:22:22:22:22"
]
}
Or just:
{
"mac_addr": "33:33:33:33:33:33"
}
Now, in the Java side I have a little PoJo (class MacAddress
) that takes a String
in the constructor, transforms it to binary and provides some nice to have methods, such as verifiers, manufacturer extractors... things like that.
I have created a Jacskson custom deserializer to, given a String
containing a MAC address, converter it to an actual instance of that MacAddress
class:
@Override
public MacAddress deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
JsonToken currentToken = jp.getCurrentToken();
if (currentToken.equals(JsonToken.VALUE_STRING)) {
return new MacAddress(jp.getValueAsString());
}
return null;
}
That can be used as an annotation in the class loaded from the RESTful API (from the JSON):
@JsonIgnoreProperties(ignoreUnknown = true)
public class Device {
@JsonDeserialize(using = MacAddressDeserializer.class)
@JsonSerialize(using = MacAddressSerializer.class)
public MacAddress mac;
// . . . moar and moar thingies
Now, the question:
Is there a way to tell Jackson (version 2.5.1, but I could increase it, if it'd help) to model a class with a List<MacAddress>
and use the same deserializer for a single one? Basically, being able to tell Jackson something along the lines of "I'm not lying to you: you're gonna be receiving a list of MAC addresses, so for each item in the list, I'd like you to use the MacAddressDeserializer.class
that I have already defined somewhere"
I have found lots of examples, but all of them explaining how to implement a deserializer for a list of objects, or for a single object, or for forcing a list even if there's only one object, but I'd like having to avoid writing two deserializers, one for MacAddress
and a second one for List<MacAddress>
. Is there a way of using the same deserializer class for both things?
Thank you in advance.