I have the following JSON, where my property can either be a String, a Number or a reference to a variable:
[
{
"prop": "string-value"
},
{
"prop": 5
},
{
"prop": {
"$variable": "var1"
}
}
]
I want to deserialize this into 3 subclasses, for this I am trying to use DEDUCTION:
@JsonTypeInfo(use = Id.DEDUCTION)
@JsonSubTypes({ @Type(MultitypeString.class), @Type(MultitypeVariable.class), @Type(MultitypeNumber.class) })
public abstract class Multitype {
}
public class MultitypeString extends Multitype {
public String value;
public MultitypeString(String value) {
this.value = value;
}
}
public class MultitypeNumber extends Multitype {
public Number value;
public MultitypeNumber(Number value) {
this.value = value;
}
}
public class MultitypeVariable extends Multitype {
@JsonIdentityReference
@JsonProperty("$variable")
public Variable variable;
}
For the variable reference this works fine but for string and numeric values I get the following error:
Subtypes MultitypeNumber and MultitypeString have the same signature and cannot be uniquely deduced
Which is weird since the signatures do vary, one is a String constructor and one is a Number constructor. Is there a way I can have the DEDUCER take the datatype into account? If not, is there another way to get what I'm trying to achieve?