I have an object
public class Point{
int x, y;
Point(int x, int y){
this.x = x;
this.y = y;
}
public String toString(){
String ret = "[";
ret += Integer.toString(x);
ret += ", ";
ret += Integer.toString(y);
ret += "]";
return ret;
}
}
I have been able to deserialize this object with Gson like so:
class PointDeserializer implements JsonDeserializer<Point>{
@Override
public Point deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
Gson gson = new Gson();
int[] tmp = gson.fromJson(json, int[].class);
int a = tmp[0];
int b = tmp[1];
return new Point(a,b);
}
}
Now, I use the following at last to make it work. Note that type and str are strings.
Class myClass = Class.forName(type);
Class myClassDeserializer = Class.forName(type + "Deserializer");
Gson gson = new GsonBuilder().registerTypeAdapter(myClass, myClassDeserializer.newInstance()).create();
Object ret = gson.fromJson(str, myClass);
Now here is the main problem. I want to do this for classes Point[], Point[][] and so on also.
Will I have to write a deserializer for every dimension of Point or is there a better way to do it?
Please help.