Search in sources :

Example 1 with JacksonModuleDefinition

use of com.serotonin.m2m2.module.JacksonModuleDefinition in project ma-core-public by infiniteautomation.

the class MangoRestSpringConfiguration method createNewObjectMapper.

/**
 * Create an instance of the Object Mapper.
 * Used locally when starting Spring but may also be used for testing.
 *
 * Note: This is NOT the same Object Mapper instance used within a running Mango.
 * XXX J.W. Seems to be used inside the REST controller to me?
 *
 * @return
 */
public static ObjectMapper createNewObjectMapper() {
    // For raw Jackson
    ObjectMapper objectMapper = new ObjectMapper();
    if (Common.envProps.getBoolean("rest.indentJSON", false))
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    // JScience
    JScienceModule jScienceModule = new JScienceModule();
    objectMapper.registerModule(jScienceModule);
    // Mango Core JSON Modules
    MangoCoreModule mangoCore = new MangoCoreModule();
    objectMapper.registerModule(mangoCore);
    MangoRestV2JacksonModule mangoCoreV2 = new MangoRestV2JacksonModule();
    objectMapper.registerModule(mangoCoreV2);
    // Setup Module Defined JSON Modules
    List<JacksonModuleDefinition> defs = ModuleRegistry.getDefinitions(JacksonModuleDefinition.class);
    for (JacksonModuleDefinition def : defs) {
        if (def.getSourceMapperType() == JacksonModuleDefinition.ObjectMapperSource.REST)
            objectMapper.registerModule(def.getJacksonModule());
    }
    // Always output dates in ISO 8601
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    objectMapper.setDateFormat(dateFormat);
    objectMapper.registerModule(new JavaTimeModule());
    // Set to system tz
    objectMapper.setTimeZone(TimeZone.getDefault());
    // This will allow messy JSON to be imported even if all the properties in it are part of the POJOs
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}
Also used : JScienceModule(com.serotonin.m2m2.web.mvc.rest.v1.mapping.JScienceModule) MangoCoreModule(com.serotonin.m2m2.web.mvc.rest.v1.mapping.MangoCoreModule) JacksonModuleDefinition(com.serotonin.m2m2.module.JacksonModuleDefinition) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) MangoRestV2JacksonModule(com.infiniteautomation.mango.rest.v2.mapping.MangoRestV2JacksonModule) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 MangoRestV2JacksonModule (com.infiniteautomation.mango.rest.v2.mapping.MangoRestV2JacksonModule)1 JacksonModuleDefinition (com.serotonin.m2m2.module.JacksonModuleDefinition)1 JScienceModule (com.serotonin.m2m2.web.mvc.rest.v1.mapping.JScienceModule)1 MangoCoreModule (com.serotonin.m2m2.web.mvc.rest.v1.mapping.MangoCoreModule)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1