我可以通过添加模块来自定义Jackson ObjectMapper吗? [英] Can I customize a Jackson ObjectMapper by adding a module?

查看:147
本文介绍了我可以通过添加模块来自定义Jackson ObjectMapper吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用库创建 ObjectMapper 并添加一些模块 s它自己对这个映射器。我希望这些模块中的序列化器能够打印出来。但是,我唯一访问配置此映射器的方法是通过构建器,允许我添加自己的模块。我可以使用我的能力将模块添加到 ObjectMapper 以将其配置为漂亮的打印吗?我没有看到任何方法或属性 SimpleModule 表示我可以。

I am using a library that creates an ObjectMapper and adds some Modules of its own to this mapper. I would like the serializers in these modules to pretty print. However, my only access to configure this mapper is via a builder that lets me add my own modules. Can I use my ability to add modules to the ObjectMapper to configure it to pretty print? I'm not seeing any methods or properties of SimpleModule suggesting that I can.

推荐答案

是的,如果您可以使用 ObjectMapper 注册模块,则可以操作映射器。覆盖模块上的 setupModule()方法,然后使用 getOwner() Module.SetupContext 上的c $ c>:

Yes, if you can register a Module with an ObjectMapper, you can manipulate the mapper. Override the setupModule() method on the Module and then use getOwner() on the Module.SetupContext that you get:

GraphSONMapper mapper = GraphSONMapper.build().addCustomModule(new SimpleModule() {                                          
    public void setupModule(com.fasterxml.jackson.databind.Module.SetupContext context) {                                    
        ObjectMapper mapper = context.getOwner();                                                                            
        mapper.enable(SerializationFeature.INDENT_OUTPUT);                                                                   
    }                                                                                                                        
}).create();                                                                                                                 
GraphSONWriter writer = graph.io().graphSONWriter().mapper(mapper).create();

getOwner()的文档建议你除非你必须这样做,否则不应这样做。

The documentation for getOwner() suggests you shouldn't do this unless you have to.

这篇关于我可以通过添加模块来自定义Jackson ObjectMapper吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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