java - 递归类型适配器 GSON

标签 java android gson

鉴于以下结构:

abstract class Message {
   Message anotherMessage;
   String attribute; //just random stuff
}

我想要以下 json 字符串作为输出:

 {type=Class.getSimpleName(), data=gson(Message)} 

as 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 对象(stackoverflow-exception)

最佳答案

可以在序列化/反序列化期间使用 JsonSerializationContext/JsonDeserializationContext 来序列化/反序列化另一个对象。

消息.java

abstract class Message {
    Message anotherMessage;
    String theMessage;

    public Message getAnotherMessage() {
        return anotherMessage;
    }

    public String getTheMessage() {
        return theMessage;
    }
}

信息.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;
    }
}

ErrorMessage.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;
}

测试.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);

}

关于java - 递归类型适配器 GSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28696040/

相关文章:

java - 测试加载消息

java - Apache POI 锁定单元格但允许调整列大小

java - Android onClick 函数不起作用

java - 如何在 Java 中使用 GSON 将 JSON 对象的映射转换为 JSON?

java - com.google.gson.JsonSyntaxException : Expected BEGIN_ARRAY but was STRING

java - 为什么 replaceFirst 和 replaceAll 给出不同的结果?

java - RecyclerView 没有附加适配器;跳过布局,数据不显示

java - 如何平滑地每秒增加一个数字?

java - 使用 GSON 在 Java 中解析 JSON

java - 在 Eclipse 中找不到 Speechlet 接口(interface)