简体   繁体   English

GSON 解析不确定类型的序列化实例

[英]GSON parse serialized instance of uncertain type

In the following example I have to parse a json string which is the serialization of an object that may be either instance of CustomClass_1 or of CustomClass_2 and I don't know it beforehand.在以下示例中,我必须解析 json 字符串,它是 object 的序列化,可能是 CustomClass_1 或 CustomClass_2 的实例,但我事先并不知道。 The problems are as follows:问题如下:

  • I get an exception com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY我得到一个异常 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_RAIN_OBJECT
  • the json string produced by GSON from the instance of CustomClass_2 is just the string ["item"] as if the object was just an instance of the superclass ArrayList the json string produced by GSON from the instance of CustomClass_2 is just the string ["item"] as if the object was just an instance of the superclass ArrayList

Any suggestion to achieve what I want?有什么建议可以实现我想要的吗?

class CustomClass_1 {
  String type;

  CustomClass_1(){
    type = "CustomClass_1";
  }
}

class CustomClass_2 extends ArrayList<String> {
  String type;

  CustomClass_2(){
    super();
    type = "CustomClass_2";
    this.add("item");
  }
}

public class DeserializationTest {

  @Test
  public void testIncomingMessageParsing(){
    String serialized_object = receive();
    CustomClass_1 cc1 = null;
    CustomClass_2 cc2 = null;
    try{
      cc1 = (new Gson()).fromJson(serialized_object, CustomClass_1.class);
    } catch (Exception e){
      e.printStackTrace();
    }
    try{
      cc2 = (new Gson()).fromJson(serialized_object, CustomClass_2.class);
    } catch (Exception e){
      e.printStackTrace();
    }
    if (cc1 != null && cc1.type.equals("CustomClass_1")) {
      System.out.println("It's a CustomClass_1.");
    } else if (cc2 != null && cc2.type.equals("CustomClass_2")) {
      System.out.println("It's a CustomClass_2.");
    }
  }

  String receive(){
    CustomClass_1 cc1 = new CustomClass_1();
    CustomClass_2 cc2 = new CustomClass_2();
    String serialized_object = "";
    Gson gson = new Gson();
    boolean head = (new Random()).nextBoolean();
    if (head){
      serialized_object = gson.toJson(cc1);
    } else {
      serialized_object = gson.toJson(cc2);
    }
    return serialized_object;
  }
} // end of class

First, the JsonSyntaxException is thrown in the "wrong" case where the data to deserialize does not match the string data, so maybe this can be ignored because the other case will deserialize the string correctly.首先,在要反序列化的数据与字符串数据不匹配的“错误”情况下抛出JsonSyntaxException ,因此可能可以忽略此情况,因为其他情况将正确反序列化字符串。 If you need to suppress this exception, then you might consider using some different approach to define your objects.如果您需要抑制此异常,那么您可能会考虑使用一些不同的方法来定义您的对象。 This might also be the case when Collection s will be serialized as objects and not as arrays and therefore the "wrong" deserialization is done.Collection s 将被序列化为对象而不是 arrays 时,也可能出现这种情况,因此完成了“错误”的反序列化。

Second, it seems that deriving from a Collection (and List ) and adding fields to derived classes, GSON will only serialize the Collection 's elements, according to this source .其次,根据这个来源,GSON 似乎从Collection (和List )派生并添加字段到派生类,只会序列化Collection的元素。 For this kind of classes, you might need a custom TypeAdapter implementation to achieve the desired JSON string.对于这种类,您可能需要自定义TypeAdapter实现来实现所需的 JSON 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM