I have the following POJOs:
public interface Shape { public double calcArea(); public double calcPerimeter(); } public class Rectangle implement Shape {
I have no problem getting GSON to serialize a ShapeHolder instance in JSON. But when I try to deserialize the string of this JSON back to the ShapeHolder instance, I get errors:
String shapeHolderAsStr = getString(); ShapeHolder holder = gson.fromJson(shapeHodlderAsStr, ShapeHolder.class);
Throws:
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
So I looked here and started implementing my own ShapeInstanceCreator :
public class ShapeInstanceCreator implements InstanceCreator<Shape> { @Override public Shape createInstance(Type type) {
But now I'm stuck: I was only given java.lang.reflect.Type , but I really need java.lang.Object so I can write the code:
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; } }
What can I do?
Refresh
As suggested by @raffian (the link that he / she posted), I implemented the InterfaceAdapter just like in the link (I did not change anything). Now I get the following exception:
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)
Any ideas?
java reflection gson type-safety deserialization
IAmYourFaja
source share