创建杰克逊ObjectMapper实例的副本 [英] Create clone of jackson ObjectMapper Instance

查看:56
本文介绍了创建杰克逊ObjectMapper实例的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要com.fasterxml.jackson.databind.ObjectMapper实例的库.库的用户应该能够提供ObjectMapper或ObjectMapper实例本身的配置.但是我还添加/修改了序列化程序的某些设置,而不会影响用户的ObjectMapper实例.

I'm writing a library that needs a com.fasterxml.jackson.databind.ObjectMapper instance. The user of the library should be able to provide the configuration for the ObjectMapper or the ObjectMapper instance itself. But I also add/modify some settings of the serializer without affecting the users ObjectMapper instance.

有什么方法可以创建ObjectMapper实例的副本/克隆吗?

Is there any way to create a copy/clone of ObjectMapper instance?

ObjectMapper clonedInstance = new ObjectMapper(originalMapper.getFactory())似乎可以工作.但是我不确定是否缺少任何东西. ObjectMapper的行为将与原始对象完全一样吗?

It looks like ObjectMapper clonedInstance = new ObjectMapper(originalMapper.getFactory()) could work. But I'm not sure if there is anything what I'm missing. Will the ObjectMapper behave exactly as the original one?

当前,这是我的代码:

public MyLibraryClass {
    private ObjectMapper internalMapper;

    public MyLibraryClass(ObjectMapper mapper) {
        if (mapper == null) {
            internalMapper = new ObjectMapper();
        } else {
            internalMapper = new ObjectMapper(mapper.getFactory());
        }
    }
}

推荐答案

您可以使用

You can use ObjectMapper#copy():

copy

copy

public ObjectMapper copy()

用于创建具有相同初始名称的新ObjectMapper实例的方法 配置为此实例.请注意,这还需要进行 基础JsonFactory实例的副本.

Method for creating a new ObjectMapper instance that has same initial configuration as this instance. Note that this also requires making a copy of the underlying JsonFactory instance.

方法通常用于多个,配置不同的映射器 是必需的.尽管配置是共享的,但缓存的序列化程序和 不共享反序列化器,这意味着新实例可能是 使用前重新配置;意味着它的行为方式就像 一个实例是从头开始构建的.

Method is typically used when multiple, differently configured mappers are needed. Although configuration is shared, cached serializers and deserializers are NOT shared, which means that the new instance may be re-configured before use; meaning that it behaves the same way as if an instance was constructed from scratch.

因为: 2.1

示例:

public MyLibraryClass {
    private ObjectMapper internalMapper;
    public MyLibraryClass(ObjectMapper mapper) {
        if (mapper == null) {
            internalMapper = new ObjectMapper();
        } else {
            internalMapper = mapper.copy();
        }
    }
}

还可以从 ObjectMapper类javadocs :

Also see this observation from the ObjectMapper class javadocs:

(...)方法copy(),该方法创建具有特定 配置,并允许在配置复制实例之前 它被使用了.请注意,copy()操作的费用与 构造一个新的ObjectMapper实例:如果可能,您应该 如果打算将多个映射器用于多个映射器,则它们仍会池化并重用 操作.

(...) method copy() which creates a clone of the mapper with specific configuration, and allows configuration of the copied instance before it gets used. Note that copy() operation is as expensive as constructing a new ObjectMapper instance: if possible, you should still pool and reuse mappers if you intend to use them for multiple operations.

这篇关于创建杰克逊ObjectMapper实例的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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