json对象数组导致hierarchicalstreamreader在解组期间读取空值或空值

bhmjp9jg  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(190)

我正在尝试使用xstream反序列化json字符串。我已经编写了反序列化代码,但是不管最初的json字段是否有值,xstreamn还是为输出发出空/空值。

private static String JSON = "{\n"
      + "  \"entity\": {\n"
      + "    \"entityType\": \"User\",\n"
      + "    \"entityId\": \"0-1\",\n"
      + "    \"aclRead\": [\n"
      + "      {\n"
      + "        \"entityId\": \"2-1\"\n"
      + "      },\n"
      + "      {\n"
      + "        \"entityId\": \"2-2\"\n"
      + "      },\n"
      + "      {\n"
      + "        \"entityId\": \"2-3\"\n"
      + "      }\n"
      + "    ],\n"
      + "    \"aclWrite\": [\n"
      + "      {\n"
      + "        \"entityId\": \"1-1\"\n"
      + "      },\n"
      + "      {\n"
      + "        \"entityId\": \"1-2\"\n"
      + "      },\n"
      + "      {\n"
      + "        \"entityId\": \"1-3\"\n"
      + "      }\n"
      + "    ],\n"
      + "    \"blobNames\": [\n"
      + "      \"picture\",\n"
      + "      \"attachment\"\n"
      + "    ],\n"
      + "    \"favorites\": [\n"
      + "      \"cow\",\n"
      + "      \"goat\",\n"
      + "      \"chicken\"\n"
      + "    ],\n"
      + "    \"average\": 95.5,\n"
      + "    \"scores\": [\n"
      + "      [\n"
      + "        \"chess\",\n"
      + "        1\n"
      + "      ],\n"
      + "      [\n"
      + "        \"go\",\n"
      + "        0.25\n"
      + "      ],\n"
      + "      [\n"
      + "        \"domino\",\n"
      + "        0.5\n"
      + "      ]\n"
      + "    ],\n"
      + "    \"nickname\": \"Scott\",\n"
      + "    \"birthDate\": \"2020-12-08 12:37:31.696 UTC\",\n"
      + "    \"age\": 40\n"
      + "  }\n"
      + "}";

和(正在)反序列化此代码:

public <T> T deserialize(String json, Class<T> tClass) {
    if (Objects.isNull(json)) {
      return null;
    }
    XStream xstream = new XStream(new JettisonMappedXmlDriver());
    xstream.processAnnotations(tClass);
    //xstream.ignoreUnknownElements();
    xstream.registerConverter(
        new EntityConverter(xstream.getMapper(), xstream.getReflectionProvider()));
    T model = (T) xstream.fromXML(json);
    return model;
  }

以及一个转换器:

@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    EntityDTO entity = new EntityDTO();
    while(reader.hasMoreChildren()) {
      reader.moveDown();
      System.out.println("===================================");
      System.out.println("Node Name : " + reader.getNodeName());
      System.out.println("Value     : " + reader.getValue());
      System.out.println("===================================");
      //context.convertAnother(entity, String.class);
      reader.moveUp();
    }
    return entity;
  }

输出:

===================================
Node Name : entityType
Value     : User
===================================
===================================
Node Name : entityId
Value     : 0-1
===================================
===================================
Node Name : aclRead
Value     : 
===================================
===================================
Node Name : aclRead
Value     : 
===================================
===================================
Node Name : aclRead
Value     : 
===================================
===================================
Node Name : aclWrite
Value     : 
===================================
===================================
Node Name : aclWrite
Value     : 
===================================
===================================
Node Name : aclWrite
Value     : 
===================================
===================================
Node Name : blobNames
Value     : picture
===================================
===================================
Node Name : blobNames
Value     : attachment
===================================
===================================
Node Name : favorites
Value     : cow
===================================
===================================
Node Name : favorites
Value     : goat
===================================
===================================
Node Name : favorites
Value     : chicken
===================================
===================================
Node Name : average
Value     : 95.5
===================================
===================================
Node Name : scores
Value     : chess
===================================
===================================
Node Name : scores
Value     : 1
===================================
===================================
Node Name : scores
Value     : go
===================================
===================================
Node Name : scores
Value     : 0.25
===================================
===================================
Node Name : scores
Value     : domino
===================================
===================================
Node Name : scores
Value     : 0.5
===================================
===================================
Node Name : nickname
Value     : Scott
===================================
===================================
Node Name : birthDate
Value     : 2020-12-08 12:37:31.696 UTC
===================================
===================================
Node Name : age
Value     : 40
===================================

如图所示 aclRead 以及 aclWrite 为空或null,但原始json显然包含值。xstream的代码或配置可能有什么问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题