Answer the question
In order to leave comments, you need to log in
Java Gson deserialization. How to handle interfaces?
I have these types:
interface IBla
{
}
class Bla implement IBla
{
private int field;
}
class Bla2 implement IBla
{
private int field;
private int field2;
}
class Foo
{
IBla kuku;
}
Serialization of objects of the Foo class proceeds normally. Answer the question
In order to leave comments, you need to log in
there is a wild idea to make Foo typed, I don’t know how correct it will be .. like this
class Foo<T extends IBla>
{
T kuku;
}
Foo foo = gson.fromJson(jsonString, new TypeToken<Foo<Bla2>>() {}.getType())
Depends on the implementation you are using. In Jackson it's done like this:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Bla.class, name = "Bla"),
@JsonSubTypes.Type(value = Bla2.class, name = "Bla2")
})
interface IBla {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question