Problem GSON i InstanceCreator

Mam następujące POJO:

public interface Shape {
    public double calcArea();
    public double calcPerimeter();
}

public class Rectangle implement Shape {
    // Various properties of a rectangle
}

public class Circle implements Shape {
    // Various properties of a circle
}

public class ShapeHolder {
    private List<Shape> shapes;

    // other stuff
}

Nie mam problemu, aby GSON serializował wystąpienieShapeHolder do JSON. Ale kiedy próbuję deserializować ciąg tego JSON z powrotem doShapeHolder na przykład otrzymuję błędy:

String shapeHolderAsStr = getString();
ShapeHolder holder = gson.fromJson(shapeHodlderAsStr, ShapeHolder.class);

Rzuca:

Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args constructor for interface    
net.myapp.Shape. Register an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.ConstructorConstructor$8.construct(ConstructorConstructor.java:167)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
    ... rest of stack trace ommitted for brevity

Więc spojrzałemtutaj i zacząłem wdrażać własneShapeInstanceCreator:

public class ShapeInstanceCreator implements InstanceCreator<Shape> {
    @Override
    public Shape createInstance(Type type) {
        // TODO: ???
        return null;
    }
}

Ale teraz utknąłem: otrzymałem tylkojava.lang.reflect.Type, ale naprawdę potrzebujęjava.lang.Object więc mogę napisać kod jak:

public class ShapeInstanceCreator implements InstanceCreator<Shape> {
    @Override
    public Shape createInstance(Type type) {
        Object obj = convertTypeToObject(type);

        if(obj instanceof Rectangle) {
            Rectangle r = (Rectangle)obj;
            return r;
        } else {
            Circle c = (Circle)obj;
            return c;
        }

        return null;
    }
}

Co mogę zrobić? Z góry dziękuję!

AKTUALIZACJA:

Sugestia Per @ raffian (link, który opublikował), zaimplementowałemInterfaceAdapter dokładnie jak ten w linku (nie zmieniłem siębyle co). Teraz otrzymuję następujący wyjątek:

Exception in thread "main" com.google.gson.JsonParseException: no 'type' member found in what was expected to be an interface wrapper
    at net.myapp.InterfaceAdapter.get(InterfaceAdapter.java:39)
    at net.myapp.InterfaceAdapter.deserialize(InterfaceAdapter.java:23)

Jakieś pomysły?

questionAnswers(2)

yourAnswerToTheQuestion