com.cerner.beadledom.jackson.ObjectMapperProvider Maven / Gradle / Ivy
package com.cerner.beadledom.jackson;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.Set;
/**
* A Guice provider for {@link ObjectMapper}.
*
* @author Nimesh Subramanian
*/
class ObjectMapperProvider implements Provider {
private Set jacksonModules;
private Set serializationFeatureFlag;
private Set deserializationFeatureFlag;
private Set mapperFeatureFlag;
private Set jsonGeneratorFeatureFlag;
private Set jsonParserFeatureFlag;
@Inject
ObjectMapperProvider(Set jacksonModules,
Set serializationFeatureFlag,
Set deserializationFeatureFlag,
Set mapperFeatureFlag,
Set jsonGeneratorFeatureFlag,
Set jsonParserFeatureFlag) {
this.serializationFeatureFlag = serializationFeatureFlag;
this.deserializationFeatureFlag = deserializationFeatureFlag;
this.mapperFeatureFlag = mapperFeatureFlag;
this.jsonGeneratorFeatureFlag = jsonGeneratorFeatureFlag;
this.jsonParserFeatureFlag = jsonParserFeatureFlag;
this.jacksonModules = jacksonModules;
}
@Override
public ObjectMapper get() {
ObjectMapper objectMapper = new ObjectMapper();
// Sets default values before looking at bound features
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_ABSENT);
objectMapper.setPropertyNamingStrategy(
PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.registerModules(jacksonModules);
for (SerializationFeatureFlag feature: serializationFeatureFlag) {
objectMapper.configure(feature.feature(), feature.isEnabled());
}
for (DeserializationFeatureFlag feature: deserializationFeatureFlag) {
objectMapper.configure(feature.feature(), feature.isEnabled());
}
for (MapperFeatureFlag feature: mapperFeatureFlag) {
objectMapper.configure(feature.feature(), feature.isEnabled());
}
for (JsonParserFeatureFlag feature: jsonParserFeatureFlag) {
objectMapper.configure(feature.feature(), feature.isEnabled());
}
for (JsonGeneratorFeatureFlag feature: jsonGeneratorFeatureFlag) {
objectMapper.configure(feature.feature(), feature.isEnabled());
}
return objectMapper;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy