Yo!
I'm trying to migrate a makeshift polymorphic custom deserializer (which i
spawned because I hadn't read the docs). The deserializer originally worked
like the following annotation combo as per Polymorphic Deserialization usage
public class Container {
private String id;
@JsonTypeInfo(use = Id.NAME, include = As.EXTERNAL_PROPERTY, property =
"id")
@JsonSubTypes({
@Type(name = "FOO", value = Foo.class),
@Type(name = "NAR", value = Nar.class)
})
private Object target;
}
Getters and setters omitted for brevity.
Basically original implementation could set the target field value as
particular type depending on id field. But in addition to that, it could
determine which field to read from, since the input source (sadly) only has
one constant field (id) and target field's name would change depending on
id field. If I was not using typeinfo and subtypes annotation combo, the
annotation for aliases would be
@JsonAlias({"fooValue", "narValue"})
On their own, the two features work fine, but when you would try to use the
two together, polymorphic deserializer is incapable of determining the
source value field via aliases. Is there any way to make polymorphism work
with field aliases?
Below is the minimum working snippet to replicate my issue
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
String json = "{ \"id\": \"NAR\", \"narValue\": {}}";
ObjectMapper mapper = new ObjectMapper();
Container container = mapper.readValue(json, Container.class);
System.out.println(container.getTarget()); // expected value is
Nar@[hashcode]
}
public static class Nar {
private String dar;
private String zar;
public String getDar() {
return dar;
}
public void setDar(String dar) {
this.dar = dar;
}
public String getZar() {
return zar;
}
public void setZar(String zar) {
this.zar = zar;
}
}
public static class Foo {
private String bar;
private String zar;
public String getBar() {
return bar;
}
public void setBar(String bar) {
this.bar = bar;
}
public String getZar() {
return zar;
}
public void setZar(String zar) {
this.zar = zar;
}
}
public static class Container {
private String id;
@JsonTypeInfo(use = Id.NAME, include = As.EXTERNAL_PROPERTY,
property = "id")
@JsonSubTypes({
@Type(name = "FOO", value = Foo.class),
@Type(name = "NAR", value = Nar.class)
})
@JsonAlias({
"fooValue",
"narValue"
})
private Object target;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
}
}
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jackson-user/2e28b484-71ad-41af-be92-ffc12ed30ae2n%40googlegroups.com.