All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sherpa.partner.api.client.util.ObjectMapperFactory Maven / Gradle / Ivy

The newest version!
package sherpa.partner.api.client.util;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.datatype.joda.JodaModule;

public final class ObjectMapperFactory {
    private static boolean failOnUnknownProperties;

    private ObjectMapperFactory() {
    }

    public static ObjectMapper make() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JodaModule());
        mapper.setPropertyNamingStrategy(new NoIsPropertyNamingStrategy());
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, failOnUnknownProperties);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        return mapper;
    }

    public static void setFailOnUnknownProperties(boolean fail) {
        failOnUnknownProperties = fail;
    }

    private static class NoIsPropertyNamingStrategy extends PropertyNamingStrategy {
        @Override
        public String nameForGetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName) {
            if (method.hasReturnType() && (method.getRawReturnType() == Boolean.class || method.getRawReturnType() == boolean.class)
                    && method.getName().startsWith("is")) {
                return method.getName();
            }
            return super.nameForGetterMethod(config, method, defaultName);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy