【问题标题】:How to turn custom seralizeable method of GSON into Generic如何将 GSON 的自定义可序列化方法转换为 Generic
【发布时间】:2012-11-02 03:14:44
【问题描述】:

我有以下代码尝试序列化 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 ?

【问题讨论】:

标签: java generics gson


【解决方案1】:

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

GsonBuilder.registerTypeHierarchyAdapter(Class&lt;?&gt; 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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2019-04-10
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多