有没有条件jackson json策略?

发布于 2024-10-31 19:56:18 字数 1263 浏览 5 评论 0原文

我需要使用 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:多个 代表财产的字段 “属性”:

那么,有没有其他方法可以使用杰克逊的注释来实现更多面向对象的操作?

亲切的问候,
暴君

I need to deserialize a GeoJSON JSON object with the Jackson library. In this json object there are places where this applies:
If there is

"type" : "name"  

than the following json object is of one type. Lets say, for example:

"properties": {
   "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}  

On the other hand, if there is

"type": "link"  

than the following json object is of another type. Lets say, for example:

"properties": {  
   "href": "http://example.com/crs/42",  
   "type": "proj4"  
}  

Now I have a PropertiesPOJO which contains:

private String name;  
private String href;
private String type;

The current solution demands checking the object's type so we know which parts of the PropertiesPOJO we want to consider. This is not a good practice. I would rather do this with a custom deserializer which will deserialize directly to NamePropertiesPOJO or LinkPropertiesPOJO. One idea that I had is to place

@JsonDeserialize(using=PropertiesDeserializer.class)
@JsonProperty("properties")
NamePropertiesPOJO namePropertiesPOJO = null;

@JsonDeserialize(using=PropertiesDeserializer.class)
@JsonProperty("properties")
LinkPropertiesPOJO linkPropertiesPOJO = null;

But I get a

JsonMappingException happened:Multiple
fields representing property
"properties":

So, is there any other way I can do this more OO using jackson's annotations?

Kind Regards,
Despot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瓜 2024-11-07 19:56:18

这被称为“外部类型 ID”,虽然 Jackson 尚不支持它(与嵌入到 Object 中的类型标识符不同;在本例中,作为一个附加属性),但应该在未来,按照 JACKSON-453。它不会出现在 1.8 中,但希望能出现在之后的版本中。
GeoJSON 是一个常见的重复用例——我希望他们选择了更常见的机制,但由于格式的流行,这是一个需要优先支持的事情。

This is known as "External type id", and while it is not yet supported by Jackson (unlike type identifiers that would be embedded in Object; in this case, as one additional property), it is something for which support should be added in future, as per JACKSON-453. It won't make it in 1.8, but hopefully for version after that.
GeoJSON is a commonly recurring use case for this -- I wish they had chosen more common mechanism, but due to popularity of format this is a high-priority thing to support.

卷耳 2024-11-07 19:56:18

您应该能够通过

class Pojo {
    Map<String,String> properties;
    ...
}

然后只需从地图上读取内容来完成此操作。

另外,当你只有没有注释的 POJO 时,Jackson 也应该
能够自行填写正确的变量。在设置器中,您可以设置一个标志,确定这是类型 1 或类型 2,这样您就可以执行 if (pojo.isType1())

You should be able to do it via

class Pojo {
    Map<String,String> properties;
    ...
}

And then just read the stuff from the map.

Also when you just have the POJO that you showed without annotations, Jackson should also
be able to fill in the right variables by itself. And in the setters you could set a flag it this is type 1 or type 2, so you can then do if (pojo.isType1())

神回复 2024-11-07 19:56:18

我想简单的答案是,没有条件反序列化 jackson json 策略。

干杯,
暴君
PS:如果有人给我一个问题的解决方案(除了“不”),我会很乐意将答案检查转移到他的答案!

I guess the simple answer would be, there is no conditional deserialization jackson json strategy.

Cheers,
Despot
P.S.: If someone gives me a solution to the problem (other than "no") I would happily transfer the answer check to his answer!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文