如何将Jackson mixin添加到阅读器而不是objectmapper? [英] How do I add a Jackson mixin to a reader and not to the objectmapper?

查看:182
本文介绍了如何将Jackson mixin添加到阅读器而不是objectmapper?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我想在程序的一个部分中使用属性序列化,但没有不同的部分。我还有一个ObjectMapper,它被广泛定制,我用于两个序列化。我的第一个倾向是使用Mixin来调整属性是否显示,但似乎你只能将它们放在ObjectMapper上,而不是放在ObjectMapper返回的阅读器上。基本上我希望能够编写的代码如下所示。

I have an object that I would like to serialize with an attribute in one part of my program, but without in a different part. I also have an ObjectMapper which is extensively customized that I use for both serializations. My first inclination was to use a Mixin to tweak if the attribute is shown, but it seems that you can only put those on the ObjectMapper and not on a reader returned by the ObjectMapper. Basically the code I would like to be able to write would look like the following.

ObjectMapper myMapper = new ObjectMapper(); // in reality there is a lot of customization
Foo foo = myMapper.reader().withMixin(Foo.class, FooMixin.class).readValue(jsonParser, Foo.class);


推荐答案

正确。但是,您无法即时更改混合:由于它们用于(de)序列化器的内省,并且结果(实际(反)序列化器)被缓存,因此必须将它们作为初始配置的一部分添加。
这就是为什么 ObjectReader ObjectWriter 都没有公开更改混合的方法:它们只允许更改可以动态更改的内容,基于每次调用。

Correct. You can not change mix-ins on-the-fly, however: since they are used for introspection of (de)serializers, and results (actual (de)serializers) are cached, they must be added as part of the initial configuration. This is why neither ObjectReader nor ObjectWriter exposes methods to change mix-ins: they only allow changing of things that can be dynamically changed, on per-call basis.

但是混合使用不是最好的方法:你考虑过使用 JSON视图?可以为每个(反)序列化单独更改正在使用的活动视图。

But perhaps mix-ins are not the best way to do this: have you considered using JSON Views instead? Active view in use can be changed separately for each (de)serialization.

这篇关于如何将Jackson mixin添加到阅读器而不是objectmapper?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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