I have a GsonFactory declared like this:
public class GsonFactory {
public Gson create() {
return new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(MyInterface.class, new MyInstanceCreator())
.create();
}
private static final GsonFactory instance = new GsonFactory();
private GsonFactory() {
}
public static Gson getInstance(){
return instance.create();
}
}
I also have the InstanceCreator class:
public class MyInstanceCreator implements InstanceCreator<MyInterface> {
@Override
public StrategySymbol createInstance(Type type) {
return new MyInterfaceImpl();
}
}
class MyClass {
private List<MyInterface> myList;
public List<MyInterface> getMyList() { return myList; }
public void setMyList(List<MyInterface> myList) { this.myList = myList; }
}
interface MyInterface {
Long getValue1();
void setValue1(Long value1);
}
class MyInterfaceImpl implements MyInterface {
private Long value1;
@Override
public Long getValue1() {
return value1;
}
@Override
public void setValue1(Long value1) {
this.value1 = value1
}
}
This code seems well implemented, but if I try to parse a JSON with value1:
MyClass obj = GsonFactory.getInstance().fromJson("{'myList': [{'value1':8}]}", MyClass.class);
The Object is returned with an instance of MyInterfaceImpl, but the field value1 is always null. As I could see, it seems like Gson looks for the fields in the interface (none) and not in the class implementing the interface.
Does anyone know how to solve this?