java – 多态对象的JSON使用者

我正在解析JSON,并且对于一个可以具有三种形式之一的结构有困难.在我的情况下,它可以是零维,一维或二维.有没有什么方法可以动态检查JSON以确定它是哪一个?或者也许消费它然后计算出它后来的东西.

结构看起来像这样,可以嵌入其他结构中.

"details":{
    "Product":"A zero-dimensional Product"
},

"details":{
    "Product":"A one-dimensional Product",
    "Dimensions": [ "Size" ],
    "Labels": [ "XS", "S", "M", "L" ]
},

"details":{
    "Product":"A two-dimensional Product",
    "Dimensions": [ "Size", "Fit" ],
    "Labels": [[ "XS", "S", "M", "L" ],[ "26", "28", "30", "32" ]]
}

我可能正在寻找的是杰克逊将永远匹配的通用课程.

翻译的东西:

{
"SomeField": "SomeValue",
...
 "details":{
  ...
 }
}

成:

class MyClass {
  String SomeField;
  ...
  AClass details;
}

是否有一个我可以定义的类,它可以是任何JSON结构或数组的通用接收者?

解决方法:

感谢Eric的评论指向我programmerbruce我设法破解它.这是我使用的代码(减少简化).

public static class Info {
  @JsonProperty("Product")
  public String product;
  // Empty in the 0d version, One entry in the 1d version, two entries in the 2d version.
  @JsonProperty("Dimensions")
  public String[] dimensions;

}

public static class Info0d extends Info {
}

public static class Info1d extends Info {
  @JsonProperty("Labels")
  public String[] labels;
}

public static class Info2d extends Info {
  @JsonProperty("Labels")
  public String[][] labels;
}

public static class InfoDeserializer extends StdDeserializer<Info> {
  public InfoDeserializer() {
    super(Info.class);
  }

  @Override
  public Info deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    Class<? extends Info> variantInfoClass = null;
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    ObjectNode root = (ObjectNode) mapper.readTree(jp);
    // Inspect the `diemnsions` field to decide what to expect.
    JsonNode dimensions = root.get("Dimensions");
    if ( dimensions == null ) {
      variantInfoClass = Info0d.class;
    } else {
      switch ( dimensions.size() ) {
        case 1:
          variantInfoClass = Info1d.class;
          break;

        case 2:
          variantInfoClass = Info2d.class;
          break;
      }
    }
    if (variantInfoClass == null) {
      return null;
    }
    return mapper.readValue(root, variantInfoClass);
  }
}

并在ObjectMapper中安装它:

// Register the special deserializer.
InfoDeserializer deserializer = new InfoDeserializer();
SimpleModule module = new SimpleModule("PolymorphicInfoDeserializerModule", new Version(1, 0, 0, null));
module.addDeserializer(Info.class, deserializer);
mapper.registerModule(module);
factory = new JsonFactory(mapper);
上一篇:Mybatis 自定义映射关系


下一篇:如何将多个POJO转换为所需格式的xml?