我需要使用 Jackson 库反序列化GeoJSON JSON 对象。在这个 json 对象中有适用的地方:
如果有
"type" : "name"
比以下 json 对象属于一种类型。比方说,例如:
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
另一方面,如果有
"type": "link"
比下面的 json 对象是另一种类型。比方说,例如:
"properties": {
"href": "http://example.com/crs/42",
"type": "proj4"
}
现在我有一个 PropertiesPOJO,其中包含:
private String name;
private String href;
private String type;
当前的解决方案需要检查对象的类型,以便我们知道要考虑 PropertiesPOJO 的哪些部分。这不是一个好习惯。我宁愿使用自定义反序列化程序来执行此操作,该反序列化程序将直接反序列化为 NamePropertiesPOJO 或 LinkPropertiesPOJO。我的一个想法是放置
@JsonDeserialize(using=PropertiesDeserializer.class)
@JsonProperty("properties")
NamePropertiesPOJO namePropertiesPOJO = null;
@JsonDeserialize(using=PropertiesDeserializer.class)
@JsonProperty("properties")
LinkPropertiesPOJO linkPropertiesPOJO = null;
但我得到一个
JsonMappingException 发生:表示属性“属性”的多个字段:
那么,有没有其他方法可以使用杰克逊的注释来做更多的面向对象?
亲切的问候,
暴君