杰克逊·映射器(Jackson Mapper)构建后
我使用 Jackson ObjectMapper
将一些 JSON 反序列化为 Java 类,我们将其称为 PlayerData
。我想向 PlayerData
类添加一些逻辑,以便在加载字段后修复一些数据。例如,一些早期的 JSON 文件过去使用“性别”标志而不是一个“性别”falg,所以如果设置了性别标志但未设置性别标志,我想将性别字段的值设置为性别字段的值。
是否可以将某种 @PostConstruct 或 @AfterLoad 注释附加到方法上?或者也许是我可以实现的接口?我没有注意到文档中的一个功能,但这似乎是一个明显的功能。
I am using the Jackson ObjectMapper
to deserialize some JSON into a Java class, which we'll call PlayerData
. I would like to add a bit of logic to the PlayerData
class to fix up some data after the fields have been loaded in. For example, some early JSON files used to use a "sex" flag instead of a "gender" falg, so if the sex flag is set but the gender flag is not set, I'd like to set the value of the gender field to be the value of the sex field.
Is there some sort of @PostConstruct or @AfterLoad annotation that I could affix to a method? Or perhaps an interface that I could implement? I didn't notice one in the documentation, but it seemed like an obvious feature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过评论中的链接找到了这个(来源:fedor.belov)。这似乎允许您运行构建后的代码。
Found this thru a link in the comments (credit: fedor.belov). This appears to allow you to run code post construct.
不支持开箱即用,但您可以轻松创建
@JsonPostDeserialize
注释,以便在反序列化后调用方法。首先,定义注解:
然后,将以下注册和实现代码添加到您的项目中:
最后,使用
addPostDeserializeSupport
修改您的ObjectMapper
实例,它将调用所有带注释的@JsonPostDeserialize
反序列化对象的方法。This is not supported out of the box, but you can easily create your
@JsonPostDeserialize
annotation for methods to be called after deserialization.First, define the annotation:
Then, add the following registration and implementation code to your project:
Finally, modify your
ObjectMapper
instance withaddPostDeserializeSupport
, it will invoke all@JsonPostDeserialize
annotated method of deserialized objects.如果您不使用@JsonCreator,那么 Jackson 将使用 setter 和 getter 方法来设置字段。
因此,如果您定义以下方法,假设您有
TimeOfDay
和LightLevel
枚举:它会起作用。
更新:您可以找到 Jackson 库的所有注释 这里。
更新2:其他解决方案:
If you're not using the
@JsonCreator
, then Jackson will use the setter and getter methods to set the fields.So if you define the following methods assuming that you have
TimeOfDay
andLightLevel
enums:it would work.
Update: You can find all of the annotations of Jackson library here.
Update2: Other solution:
这实际上已经被建议过几次了。因此,也许提交 RFE 是有意义的;有多种方法可以实现这一点:明显的方法是注释类型(@JsonPostProcess(Processor.class))的能力以及通过 Module API 注册后处理器的能力(这样当 Jackson 构造反序列化器时基本上有一个回调,让模块指定要使用的后处理器(如果有)。但也许还有更好的方法来做到这一点。
This is something that has actually been suggested couple of times earlier. So maybe filing an RFE would make sense; there are multiple ways in which this could work: obvious ones being ability to annotate type (@JsonPostProcess(Processor.class)) and ability to register post-processor through Module API (so that there's basically a callback when Jackson constructs deserializer, to let module specify post-processor to use if any). But perhaps there are even better ways to do this.