I created a REST service that returns an ExceptionEntity serialized class as a result if something went wrong.
I want to make some kind of exception if the json that should be deserialized by Gson.fromJson() is of a different type. For example, I have this line that needs to be deserialized (my.ExceptionEntity.class):
{"exceptionId":2,"message":"Room aaaa already exists."}
but I use the Room class as the type for this serialized string:
String json = "{\"exceptionId\":2,\"message\":\"Room aaaa already exists.\"}"; Room r = gson.fromJson(json, Room.class);
[EDIT] I tested this and it does not work:
try { return g.fromJson(roomJson, new TypeToken<Room>(){}.getType());
pepuch
source share