Hi there
I am having a json equal to:
{
"bars" : [
{
},
"baz" : "..."
},
{
},
"qux" : "..."
}
]
} and some java classes like:
public static class Gna {
@JsonProperty("bars")
public List<Bar> bars;
}
public static class Foo { @JsonProperty("href") public String href;}
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include= JsonTypeInfo.As.PROPERTY, property="foo")
@JsonTypeIdResolver(BaseResolver.class)
public static abstract class BaseType {
@JsonProperty("foo")
public Foo foo;
}
public static class Baz extends BaseType {
@JsonProperty("baz") public String baz;
}
public static class Qux extends BaseType {
@JsonProperty("qux") public String qux;
}
public static class
BaseTypeResolver implements TypeIdResolver {
... @Override
public JavaType typeFromId(String s) {
// Here i get "{"
...
}
...
}
Trying to deserialize the above json to fails with the exception:
Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'foo' that is to contain type id (for class BaseType)
at [Source: N/A; line: -1, column: -1] (through reference chain: Gna["bars"]->java.util.ArrayList[0])
It is clear to me that i somehow have to point to the "href" attribute within Foo but it is not clear to me how i can do that. Is that even supported through JsonTypeInfo?
According to the documentation (
http://wiki.fasterxml.com/JacksonPolymorphicDeserialization#line-120) it should be possible with the use of the @JsonTypeResolver.
If the reference of a typeid cannot be taken from within a nested object, is there a somehow alternative elegant way to achieve this? Writing a custom deserializer
seems like a quiet bad solution to me.
Thanks for helping
rgds julien