在Jackson中序列化没有数据的对象 [英] Serialize an object with no data in Jackson

查看:138
本文介绍了在Jackson中序列化没有数据的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用注释序列化Jackson中没有字段的对象?当我尝试序列化没有注释的这样一个对象时,我得到:

Is it possible to serialize an object with no fields in Jackson using only annotations? When I attempt to serialize such an object with no annotations I get:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class [redacted].SubjectObjectFeatureExtractor and no properties discovered to create BeanSerializer 

我已经检查了杰克逊注释列表,却没有看到注释方法class没有可序列化的数据。我尝试在空构造函数上放置 @JsonCreator (不要期望它工作,因为它是反序列化注释),我得到了同样的错误。没有访问者或字段可以放入 @JsonProperty 。任何想法?

I have examined the list of Jackson annotations without seeing a way to annotate the class as having no serializable data. I tried putting @JsonCreator on the empty constructor (not expecting it to work, since it's a deserialization annotation), and I got the same error. There are no accessors or fields to put @JsonProperty on. Any ideas?

更新:这样做的原因是我有一个对象列表,表示可以应用于某种类型的转换数据的。其中一些转换是由需要序列化的参数定义的,但其中一些是无参数的(所讨论的无数据对象)。我希望能够序列化和反序列化这些转换的序列。此外,我正在使用 DefaultTyping.NON_FINAL ,以便序列化类名。

Update: The reason for this is that I have a list of objects which represent transformations which can be applied to a certain type of data. Some of these transformations are defined by parameters which needs to be serialized, but some of these are parameter-less (the data-less objects in question). I'd like to be able to serialize and deserialize a sequence of these transformations. Also, I'm using DefaultTyping.NON_FINAL so that the class name will be serialized.

更新示例类将是

class ExtractSomeFeature implements FeatureExtractor<SomeOtherType> {
    public void extractFeature(SomeOtherType obj, WeightedFeatureList output) {
          // do stuff
    }
 }

我并不特别关心这个JSON的样子,只要我可以反序列化 List< FeatureExtractor> 适当的。我的印象是使用默认输入,预期的JSON类似于:

I don't particularly care how the JSON for this looks like, as long as I can deserialize List<FeatureExtractor>s properly. My impression is that using default typing, the expected JSON would be something like:

['com.mycompany.foo.ExtractSomeFeature', {}]

其他子类FeatureExtractor 会有真实的参数,所以它们可能看起来像:

Other sub-classes of FeatureExtractor would have real parameters, so they would presumably look something like:

[`com.mycompany.foo.SomeParameterizedFeature', {some actual JSON stuff in here}]

我想我可以使用 @ JsonValue 在一些 toJSONString()方法中返回 {} ,但如果可能,我' d喜欢向最终用户隐藏这样的hackery,他们将创建 FeatureExtractor 子类。

I think I could use @JsonValue on some toJSONString() method to return {}, but if possible I'd like to hide such hackery from end-users who will be creating FeatureExtractor sub-classes.

推荐答案

在类上添加以下注释似乎解决了这个问题:

Adding the following annotation onto the class seems to solve the problem:

@JsonAutoDetect(fieldVisibility=JsonAutoDetect.Visibility.NONE)

添加无关的注释,如

@JsonRootName("fred")

似乎也解决了这个问题。这似乎与JIRA票证中的声明相符,即在该类中添加任何Jackson注释将阻止该例外。但是,似乎在类中添加注释不会。

also seems to fix it. This seems to match the claim in the JIRA ticket that adding any Jackson annotation to the class will prevent the exception. However, it appears adding annotations within the class does not.

这篇关于在Jackson中序列化没有数据的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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