递归类型适配器 GSON
Recursive Type-Adapter GSON
给定以下结构:
abstract class Message {
Message anotherMessage;
String attribute; //just random stuff
}
我想将以下 json 字符串作为输出:
{type=Class.getSimpleName(), data=gson(Message)}
因为消息是抽象的,可以有多种实现。问题是,"anotherMessage" 没有结构类型,数据。
我的序列化实现:
public JsonElement serialize(final Message src, final Type typeOfSrc,
final JsonSerializationContext context) {
Gson gson = new Gson();
JsonObject elem = new JsonObject();
elem.addProperty("type", src != null ? src.getClass().getSimpleName() : null);
elem.addProperty("data", src != null ? gson.toJson(src) : null);
return elem;
}
如何递归执行此操作?我无法获得已附加消息适配器的 Gson 对象(Whosebug 异常)
可以在 serialization/deserialization 期间使用 JsonSerializationContext/JsonDeserializationContext 到 serialize/deserialize 另一个对象。
Message.java
abstract class Message {
Message anotherMessage;
String theMessage;
public Message getAnotherMessage() {
return anotherMessage;
}
public String getTheMessage() {
return theMessage;
}
}
Info.java
public class InfoMessage extends Message {
public InfoMessage(Message anotherMessage, String theMessage) {
this.anotherMessage = anotherMessage;
this.theMessage = theMessage;
}
}
Alert.java
public class AlertMessage extends Message {
public AlertMessage(Message anotherMessage, String theMessage) {
this.anotherMessage = anotherMessage;
this.theMessage = theMessage;
}
}
错误Message.java
public class ErrorMessage extends Message {
public ErrorMessage(Message anotherMessage, String theMessage) {
this.anotherMessage = anotherMessage;
this.theMessage = theMessage;
}
}
MessageSerializer.java
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject elem = new JsonObject();
if (src == null) {
} else {
elem.addProperty("type", src.getClass().getSimpleName());
elem.addProperty("attribute", src.getTheMessage());
elem.add("data", src.anotherMessage != null ? context.serialize(src.anotherMessage, Message.class): null);
}
return elem;
}
Test.java
public static void main(String[] args) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Message.class, new MessageSerializer())
.setPrettyPrinting()
.create();
String json = gson.toJson(
new InfoMessage(
new AlertMessage(
new ErrorMessage(null, "the error message"),
"the alert message"),
"the info message"),
Message.class);
System.out.println(json);
}
给定以下结构:
abstract class Message {
Message anotherMessage;
String attribute; //just random stuff
}
我想将以下 json 字符串作为输出:
{type=Class.getSimpleName(), data=gson(Message)}
因为消息是抽象的,可以有多种实现。问题是,"anotherMessage" 没有结构类型,数据。
我的序列化实现:
public JsonElement serialize(final Message src, final Type typeOfSrc,
final JsonSerializationContext context) {
Gson gson = new Gson();
JsonObject elem = new JsonObject();
elem.addProperty("type", src != null ? src.getClass().getSimpleName() : null);
elem.addProperty("data", src != null ? gson.toJson(src) : null);
return elem;
}
如何递归执行此操作?我无法获得已附加消息适配器的 Gson 对象(Whosebug 异常)
可以在 serialization/deserialization 期间使用 JsonSerializationContext/JsonDeserializationContext 到 serialize/deserialize 另一个对象。
Message.java
abstract class Message {
Message anotherMessage;
String theMessage;
public Message getAnotherMessage() {
return anotherMessage;
}
public String getTheMessage() {
return theMessage;
}
}
Info.java
public class InfoMessage extends Message {
public InfoMessage(Message anotherMessage, String theMessage) {
this.anotherMessage = anotherMessage;
this.theMessage = theMessage;
}
}
Alert.java
public class AlertMessage extends Message {
public AlertMessage(Message anotherMessage, String theMessage) {
this.anotherMessage = anotherMessage;
this.theMessage = theMessage;
}
}
错误Message.java
public class ErrorMessage extends Message {
public ErrorMessage(Message anotherMessage, String theMessage) {
this.anotherMessage = anotherMessage;
this.theMessage = theMessage;
}
}
MessageSerializer.java
public JsonElement serialize(Message src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject elem = new JsonObject();
if (src == null) {
} else {
elem.addProperty("type", src.getClass().getSimpleName());
elem.addProperty("attribute", src.getTheMessage());
elem.add("data", src.anotherMessage != null ? context.serialize(src.anotherMessage, Message.class): null);
}
return elem;
}
Test.java
public static void main(String[] args) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(Message.class, new MessageSerializer())
.setPrettyPrinting()
.create();
String json = gson.toJson(
new InfoMessage(
new AlertMessage(
new ErrorMessage(null, "the error message"),
"the alert message"),
"the info message"),
Message.class);
System.out.println(json);
}