Search in sources :

Example 1 with KeyDeserializer

use of com.fasterxml.jackson.databind.KeyDeserializer in project airlift by airlift.

the class ObjectMapperProvider method get.

@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper(jsonFactory);
    // ignore unknown fields (for backwards compatibility)
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    // do not allow converting a float to an integer
    objectMapper.disable(DeserializationFeature.ACCEPT_FLOAT_AS_INT);
    // use ISO dates
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    // Skip fields that are null or absent (Optional) when serializing objects.
    // This only applies to mapped object fields, not containers like Map or List.
    objectMapper.setDefaultPropertyInclusion(JsonInclude.Value.construct(JsonInclude.Include.NON_ABSENT, JsonInclude.Include.ALWAYS));
    // disable auto detection of json properties... all properties must be explicit
    objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_FIELDS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
    objectMapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
    objectMapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
    objectMapper.disable(MapperFeature.INFER_PROPERTY_MUTATORS);
    objectMapper.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS);
    if (jsonSerializers != null || jsonDeserializers != null || keySerializers != null || keyDeserializers != null) {
        SimpleModule module = new SimpleModule(getClass().getName(), new Version(1, 0, 0, null, null, null));
        if (jsonSerializers != null) {
            for (Entry<Class<?>, JsonSerializer<?>> entry : jsonSerializers.entrySet()) {
                addSerializer(module, entry.getKey(), entry.getValue());
            }
        }
        if (jsonDeserializers != null) {
            for (Entry<Class<?>, JsonDeserializer<?>> entry : jsonDeserializers.entrySet()) {
                addDeserializer(module, entry.getKey(), entry.getValue());
            }
        }
        if (keySerializers != null) {
            for (Entry<Class<?>, JsonSerializer<?>> entry : keySerializers.entrySet()) {
                addKeySerializer(module, entry.getKey(), entry.getValue());
            }
        }
        if (keyDeserializers != null) {
            for (Entry<Class<?>, KeyDeserializer> entry : keyDeserializers.entrySet()) {
                module.addKeyDeserializer(entry.getKey(), entry.getValue());
            }
        }
        modules.add(module);
    }
    for (Module module : modules) {
        objectMapper.registerModule(module);
    }
    return objectMapper;
}
Also used : Version(com.fasterxml.jackson.core.Version) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) KeyDeserializer(com.fasterxml.jackson.databind.KeyDeserializer)

Aggregations

Version (com.fasterxml.jackson.core.Version)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 KeyDeserializer (com.fasterxml.jackson.databind.KeyDeserializer)1 Module (com.fasterxml.jackson.databind.Module)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)1