如何将GSON的自定义可分区方法转换为Generic

时间:2012-11-02 03:14:45

标签: java generics gson

我有以下代码尝试序列化DT_RowId。实际上我将创建一个具有DT_RowId变量的抽象类。扩展抽象类的任何其他实体也应该序列化该字段。课程如下:

Entity.java

public class Entity extends PersistentObject {
    protected long creationTime;
    protected boolean enabled;
    protected long id; // from PersistentObject
    protected long loginDuration;
    protected boolean online;
    protected String userName;
    protected long DT_RowId;// from PersistentObject
}

EntityJsonSerializer.java

import java.lang.reflect.Type;
import com.google.gson.*;

public class EntityJsonSerializer implements JsonSerializer<Entity> {
    @Override
    public JsonElement serialize(Entity entity, Type typeOfSrc, JsonSerializationContext context) {
       entity.DT_RowId = entity.id;
       Gson gson = new Gson();
       return gson.toJsonTree(entity);
    }
}

JSONTest.java

import static org.junit.Assert.*;
import org.junit.Test;
import com.google.gson.*;

public class JSONTest {
    @Test
    public final void testSerializeWithDTRowId() {
        Entity entity = new Entity();
        entity.creationTime = 0;
        entity.enabled = true;
        entity.id = 1;
        entity.loginDuration = 0;
        entity.online = false;
        entity.userName = "someone";

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Entity.class, new EntityJsonSerializer());
        Gson gson = builder.create();
        String json = gson.toJson(entity);
        String expectedJson = "{\"creationTime\":0,\"enabled\":true,\"id\":1,\"loginDuration\":0,\"online\":false,\"userName\":\"someone\",\"DT_RowId\":1}";
        assertEquals(expectedJson, json);
    }
}

如何使上述代码具有通用性,以便扩展抽象类的任何类都将DT_RowId序列化?

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法完成此操作:

GsonBuilder.registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter)

而不是:

GsonBuilder.registerTypeAdapter(Type type, Object typeAdapter)

我重构了上面的代码并且JUnit测试通过了。

PersistentObject.java

public abstract class PersistentObject {
    protected long id;
    protected long DT_RowId;
}

Entity.java

public class Entity extends PersistentObject {
    protected long creationTime;
    protected boolean enabled;
    protected long loginDuration;
    protected boolean online;
    protected String userName;
}

PersistentObjectJsonSerializer.java

import java.lang.reflect.Type;
import com.google.gson.*;

public class PersistentObjectJsonSerializer implements JsonSerializer<PersistentObject> {
    @Override
    public JsonElement serialize(PersistentObject persistentObject, Type typeOfSrc, JsonSerializationContext context) {
        persistentObject.DT_RowId = persistentObject.id;
        Gson gson = new Gson();
        return gson.toJsonTree(persistentObject);
    }
}

JSONTest.java

import static org.junit.Assert.*;
import org.junit.Test;
import com.google.gson.*;

public class JSONTest {
    @Test
    public final void testSerializeWithDTRowId() {
        Entity entity = new Entity();
        entity.id = 1;
        entity.creationTime = 0;
        entity.enabled = true;
        entity.loginDuration = 0;
        entity.online = false;
        entity.userName = "someone";

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeHierarchyAdapter(PersistentObject.class, new PersistentObjectJsonSerializer());
        Gson gson = builder.create();
        String json = gson.toJson(entity);
        String expectedJson = "{\"creationTime\":0,\"enabled\":true,\"loginDuration\":0,\"online\":false,\"userName\":\"someone\",\"id\":1,\"DT_RowId\":1}";
        assertEquals(expectedJson, json);
    }
}