默认情况下使用Jackson解析为子类 [英] Parse to a Subclass by default with Jackson

查看:387
本文介绍了默认情况下使用Jackson解析为子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Product的类和一些扩展它的子类。现在在我的注释中我有很多类型,如:

I have a class called Product and some subclasses extending it. Now in my annotations I have many types, like this:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({@Type(value=RoomProduct.class, name="RoomProduct"),
           @Type(value=GroundProduct.class, name="GroundProduct"),
           })

然后是我的Product类的定义。我想要做的是,如果Jackson无法检测到该字段不符合任何这些结构,则返回

And then the definition of my Product class. What I want to do is if Jackson cannot detect that the field is not complying with any of these structures, to return an


UnknownProduct

UnknownProduct

如何使用Jackson @Type注释做到这一点?这应该就像把名字空白或者一些我不知道的价值标志(我已经尝试创建了UnknownProduct,它扩展了Product并且没有在名称值中放置任何内容而没有成功。

How can I do that with Jackson @Type annotation? It should be something like putting blank in name or some flag in value which I don't really know (I have tried creating the UnknownProduct which extends Product and putting nothing in the name value with no success.

推荐答案

@JsonTypeInfo有一个指定默认实现类的选项,但经过一些调试后我发现WrapperObject的'defaultImpl'被破坏了。配置:

@JsonTypeInfo has an option to specify default implementation class but after some debugging I found that 'defaultImpl' is broken for the WrapperObject. Configuration:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.WRAPPER_OBJECT, defaultImpl = UnknownProduct.class)

杰克逊实施(AsWrapperTypeDeserializer):

Jackson implementation (AsWrapperTypeDeserializer):

public AsWrapperTypeDeserializer(JavaType bt, TypeIdResolver idRes,
        String typePropertyName, boolean typeIdVisible, Class<?> defaultImpl)
{
    super(bt, idRes, typePropertyName, typeIdVisible, null);
}

请注意'defaultImpl'i s已通过,但会被忽略,并且不会使用配置的默认类。我没有在jackson的Jira中找到这个问题的记录票。

Note that 'defaultImpl' is passed but it is ignored and configured default class will NOT be used. I didn't find the logged ticket for this problem in jackson's Jira.

这只是WRAPPER_OBJECT的问题,defaultImpl对其他格式工作正常。但它会改变JSON格式。如果你可以改变它 - 你可以使用EXTERNAL_PROPERTY作为默认实现:

This is a problem ONLY for the WRAPPER_OBJECT, defaultImpl is working fine for other formats. But it will change JSON format. If you can change it -- you can use EXTERNAL_PROPERTY for example with default implementation:

@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type", defaultImpl = UnknownProduct.class)

另一个解决方案:
如果你必须使用WRAPPER_OBJECT,那么你可以配置Jackson在发现未知子类型时不会失败:

Another solution: If you have to use WRAPPER_OBJECT then you can configure Jackson not to fail when it find unknown SubType:

objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);

这与您提出的要求不完全相同,但在这种情况下,您的产品将为空。可能你可以将null视为未知产品。

It is not completely the same what you are asking but in that case your product will be null. Probably you can treat null as unknown product.

更新
我已提交杰克逊错误: https://github.com/FasterXML/jackson-databind/issues/656

更新此票据已针对2.3和2.4 jackson解决,希望很快您就可以在将罐子重新安装到maven存储库或者新版本。

Update This ticket was resolved for 2.3 and 2.4 jackson and hopefully soon you should be able to use it when jars will be re-installed into the maven repository or in a new version.

这篇关于默认情况下使用Jackson解析为子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆