8

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.

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

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?

Currently this is my code:

public MyLibraryClass {
    private ObjectMapper internalMapper;

    public MyLibraryClass(ObjectMapper mapper) {
        if (mapper == null) {
            internalMapper = new ObjectMapper();
        } else {
            internalMapper = new ObjectMapper(mapper.getFactory());
        }
    }
}
Raman
  • 548
  • 1
  • 7
  • 17
  • `class YourMapper extends ObjectMapper` and write your own features – Yuriy Tsarkov Oct 25 '19 at 12:00
  • 3
    The .copy() method of ObjectMapper provides the functionality you requested. – groovedigga Oct 25 '19 at 12:27
  • But thats not what I want. I want that someone can provide the existing ObjectMapper and I want to modify it (like adding `objectMapper.setAnnotationIntrospector(...)`). I am not asking how to extend an ObjectMapper. I am asking how to create a clone of an existing instance – Raman Oct 25 '19 at 12:28
  • @groovedigga thanks! Somehow I didn't see this – Raman Oct 25 '19 at 12:29
  • 1
    @Raman: Glad that I could help. Please upvote comments that add something useful to your question. – groovedigga Oct 25 '19 at 15:06
  • @groovedigga I already upvoted your comment – Raman Oct 26 '19 at 16:03

1 Answers1

14

You can use ObjectMapper#copy():

copy

public ObjectMapper copy()

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.

Since: 2.1

Example:

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

Also see this observation from the ObjectMapper class javadocs:

(...) 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.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304