[英]How to turn custom seralizeable method of GSON into Generic
我有以下代碼嘗試序列化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?
您可以使用以下方法完成此操作:
GsonBuilder.registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter)
代替:
GsonBuilder.registerTypeAdapter(Type type, Object typeAdapter)
我已經重構了上面的代碼,並且JUnit測試通過了。
public abstract class PersistentObject {
protected long id;
protected long DT_RowId;
}
public class Entity extends PersistentObject {
protected long creationTime;
protected boolean enabled;
protected long loginDuration;
protected boolean online;
protected String userName;
}
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);
}
}
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.