I have an ObjectMapper
, which is considered the standard one, with several options set. It is defined like this:
ObjectMapper standardMapper = new ObjectMapper();
standardMapper.setSerializationInclusion(Include.NON_NULL);
standardMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE);
final SimpleModule module = new SimpleModule();
module.addSerializer(Double.class, new MyDoubleSerializer());
standardMapper.registerModule(module);
standardMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
Later I have two ObjectMappers
: the standardMapper
from above and a specificMapper
which can have any customization possible. The goal is to return an ObjectMapper
that has all the customziation of specificMapper
, but if they conflict with the ones from standardMapper
, then standardMapper
customziation should be used.
So I tried to write it like this:
public static ObjectMapper getMergedMapper(ObjectMapper standardMapper, ObjectMapper specificMapper) {
ObjectMapper mergedMapper = specificMapper.copy();
mergedMapper.setSerializationInclusion(standardMapper.getSerializationInclusion()); // getter does not exist
mergedMapper.setPropertyNamingStrategy(standardMapper.getPropertyNamingStrategy());
mergedMapper.registerModules(standardMapper.getRegisteredModules()); // getter does not exist
}
But since there are no getters for SerializationInclusion
and RegisteredModules
, what can I do? I know I could copy-paste part of the code from above (like setSerializationInclusion(Include.NON_NULL)
), but there must be a better solution…