[英]How to turn custom seralizeable method of GSON into Generic
I have the following code which tries to serialize DT_RowId. 我有以下代码尝试序列化DT_RowId。 In fact I will be creating an abstract class that has DT_RowId variable.
实际上,我将创建一个具有DT_RowId变量的抽象类。 Any other entity which extends the abstract class should also serialize the field.
扩展抽象类的任何其他实体也应序列化该字段。 The classes are as follows:
这些类如下:
Entity.java 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 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 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);
}
}
How can I make the above code generic so that any class which extends abstract class will have the DT_RowId serialized ? 如何使上面的代码通用,以便扩展抽象类的任何类都可以序列化DT_RowId?
You can accomplish this by using: 您可以使用以下方法完成此操作:
GsonBuilder.registerTypeHierarchyAdapter(Class<?> baseType, Object typeAdapter)
instead of: 代替:
GsonBuilder.registerTypeAdapter(Type type, Object typeAdapter)
I've refactored the code above and the JUnit test passes. 我已经重构了上面的代码,并且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.