I need to send objects from one socket to an other and I want to use JSON for serialization. It's necessary that every json object stores its type. My plan was to wrap my objects:
{
/* the type */ "_type_": "my.package.MyClass",
/* the actual object */ "_data_": {
...
}
}
I tried to implement this by writing this serialization adapter
Gson gson = new GsonBuilder().registerTypeAdapter(Wrapper.class, new ObjectWrapperAdapter()).create();
gson.toJson(new Wrapper(myObject), Wrapper.class);
private static class ObjectWrapperAdapter implements JsonSerializer<Wrapper>, JsonDeserializer<Wrapper> {
private static final String TYPE_KEY = "__type__";
private static final String DATA_KEY = "__data__";
@Override
public Wrapper deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
if(!json.isJsonObject()) throw new JsonParseException("element is not an object!");
JsonObject object = json.getAsJsonObject();
if(!object.has(TYPE_KEY)) throw new JsonParseException(TYPE_KEY + " element is missing!");
if(!object.has(DATA_KEY)) throw new JsonParseException(DATA_KEY + " element is missing!");
JsonElement dataObject = object.get(DATA_KEY);
String clazzName = object.get(TYPE_KEY).getAsString();
Class<?> clazz = classForName(clazzName);
return new Wrapper(context.deserialize(dataObject, clazz));
}
private Class<?> classForName(String name) throws JsonParseException {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
@Override
public JsonElement serialize(Wrapper src, Type type, JsonSerializationContext context) {
JsonObject wrapper = new JsonObject();
Object data = src.object;
JsonElement dataElement = context.serialize(data);
String className = data.getClass().getName();
wrapper.addProperty(TYPE_KEY, className);
wrapper.add(DATA_KEY, dataElement);
return wrapper;
}
}
public static class Wrapper {
private final Object object;
public Wrapper(Object object) {
this.object = object;
}
}
In theory, this works, but it fails when I try to serialize nested objects, something like this
class MyType {
AnotherType anotherType;
}
because only MyType
will be wrapped and the resulting json will look something like this:
{
"__type__": "my.package.MyType",
"__data__": {
(No "__type__" field here...)
...
}
}
Is it possible to serialize objects with their types like this?