Example usage for com.fasterxml.jackson.databind ObjectMapper registerModule

List of usage examples for com.fasterxml.jackson.databind ObjectMapper registerModule

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper registerModule.

Prototype

public ObjectMapper registerModule(Module module) 

Source Link

Document

Method for registering a module that can extend functionality provided by this mapper; for example, by adding providers for custom serializers and deserializers.

Usage

From source file:org.springframework.cloud.dataflow.server.stream.SkipperStreamDeployer.java

public static List<AppStatus> deserializeAppStatus(String platformStatus) {
    try {/*from  w ww  . j a v  a 2s.  c  o m*/
        if (platformStatus != null) {
            ObjectMapper mapper = new ObjectMapper();
            mapper.addMixIn(AppStatus.class, AppStatusMixin.class);
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            SimpleModule module = new SimpleModule("CustomModel", Version.unknownVersion());
            SimpleAbstractTypeResolver resolver = new SimpleAbstractTypeResolver();
            resolver.addMapping(AppInstanceStatus.class, AppInstanceStatusImpl.class);
            module.setAbstractTypes(resolver);
            mapper.registerModule(module);
            TypeReference<List<AppStatus>> typeRef = new TypeReference<List<AppStatus>>() {
            };
            return mapper.readValue(platformStatus, typeRef);
        }
        return new ArrayList<AppStatus>();
    } catch (Exception e) {
        logger.error("Could not parse Skipper Platform Status JSON [" + platformStatus + "]. "
                + "Exception message = " + e.getMessage());
        return new ArrayList<AppStatus>();
    }
}

From source file:com.nissatech.proasense.eventplayer.partnerconfigurations.HellaConfiguration.java

/**
 * {@inheritDoc}//www .j ava  2s  . c  om
 */
@Override
public String generateMessage(Row row) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.setDateFormat(new ISO8601DateFormat());
    HashMap message = new HashMap();
    DateTime date = new DateTime(row.getDate("variable_timestamp"));
    message.put("variable_timestamp", date);
    message.put("values", row.getMap("values", String.class, String.class));
    message.put("variable_type", row.getString("variable_type"));
    return mapper.writeValueAsString(message);

}

From source file:keywhiz.cli.CliModule.java

@Provides
public ObjectMapper generalMapper() {
    /**/*from   ww w  . j  av  a 2s .c o  m*/
     * Customizes ObjectMapper for common settings.
     *
     * @param objectMapper to be customized
     * @return customized input factory
     */
    ObjectMapper objectMapper = Jackson.newObjectMapper();
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModules(new JavaTimeModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}

From source file:org.mayocat.accounts.store.jdbi.mapper.TenantMapper.java

@Override
public Tenant map(int index, ResultSet result, StatementContext statementContext) throws SQLException {
    String slug = result.getString("slug");
    String defaultHost = result.getString("default_host");
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new GuavaModule());
    Integer configurationVersion = result.getInt("configuration_version");
    TenantConfiguration configuration;/*from   w  ww  . ja  v a2 s .  c o m*/
    if (Strings.isNullOrEmpty(result.getString("configuration"))) {
        configuration = new TenantConfiguration(configurationVersion,
                Collections.<String, Serializable>emptyMap());
    } else {
        try {
            Map<String, Serializable> data = mapper.readValue(result.getString("configuration"),
                    new TypeReference<Map<String, Object>>() {
                    });
            configuration = new TenantConfiguration(configurationVersion, data);
        } catch (IOException e) {
            final Logger logger = LoggerFactory.getLogger(TenantMapper.class);
            logger.error("Failed to load configuration for tenant with slug [{}]", e);
            configuration = new TenantConfiguration();
        }
    }

    Tenant tenant = new Tenant((UUID) result.getObject("id"), slug, configuration);
    tenant.setFeaturedImageId((UUID) result.getObject("featured_image_id"));
    tenant.setSlug(slug);
    tenant.setDefaultHost(defaultHost);
    tenant.setCreationDate(result.getTimestamp("creation_date"));
    tenant.setName(result.getString("name"));
    tenant.setDescription(result.getString("description"));
    tenant.setContactEmail(result.getString("contact_email"));

    return tenant;
}

From source file:net.codestory.http.extensions.CustomObjectMapperTest.java

@Test
public void add_resolver() {
    configure(routes -> routes.add(PersonResource.class).setExtensions(new Extensions() {
        @Override/*from   ww  w.  ja  v  a2s  .c om*/
        public ObjectMapper configureOrReplaceObjectMapper(ObjectMapper defaultObjectMapper, Env env) {
            defaultObjectMapper.registerModule(new CustomTypesModule());
            return defaultObjectMapper;
        }
    }));

    get("/person/Bob/town").should().contain("Paris");
    get("/person/John/town").should().contain("NYC");
    get("/person/Jane/town").should().respond(404);
}

From source file:com.nissatech.proasense.eventplayer.partnerconfigurations.HellaConfiguration.java

/**
 * {@inheritDoc}//  w  w  w .  j  av a 2  s.c  o  m
 */
@Override
public String generateBatch(ResultSet rows) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.setDateFormat(new ISO8601DateFormat());
    List<HashMap> batch = new ArrayList<HashMap>();
    for (Row row : rows) {
        HashMap message = new HashMap();
        DateTime date = new DateTime(row.getDate("variable_timestamp"));
        message.put("variable_timestamp", date);
        message.put("values", row.getMap("values", String.class, String.class));
        message.put("variable_type", row.getString("variable_type"));
        batch.add(message);
    }
    return mapper.writeValueAsString(batch);
}

From source file:org.lable.rfc3881.auditlogger.serialization.ReferenceableSerializerTest.java

@Test
public void basicTest() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new ReferenceableSerializerModule());

    CodeReference codeReference = new CodeReference("CS", "CodeSystem", "001", "One", "ONE");

    String result = objectMapper.writeValueAsString(codeReference);

    assertThat(result, is("{" + "\"cs\":\"CS\"," + "\"code\":\"001\"," + "\"csn\":\"CodeSystem\","
            + "\"dn\":\"One\"," + "\"ot\":\"ONE\"" + "}"));
}

From source file:org.springframework.social.strava.api.impl.StravaTemplate.java

@Override
protected MappingJackson2HttpMessageConverter getJsonMessageConverter() {
    MappingJackson2HttpMessageConverter converter = super.getJsonMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new StravaModule());
    converter.setObjectMapper(objectMapper);
    return converter;
}

From source file:com.hp.autonomy.frontend.find.core.search.QueryRestrictionsDeserializer.java

protected ObjectMapper createObjectMapper() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    return objectMapper;
}

From source file:com.strandls.alchemy.rest.client.ExceptionObjectMapperModule.java

/**
 * Binding for throwable exception mapper.
 *
 * @param mapper//from  w ww. ja v  a  2s. c  o  m
 * @return
 */
@Provides
@Singleton
@ThrowableObjectMapper
public ObjectMapper getExceptionObjectMapper(final ObjectMapper mapper) {
    // can't copy owing to bug -
    // https://github.com/FasterXML/jackson-databind/issues/245
    final ObjectMapper exceptionMapper = mapper;
    exceptionMapper.registerModule(new SimpleModule() {
        /**
         * The serial version id.
         */
        private static final long serialVersionUID = 1L;

        /*
         * (non-Javadoc)
         * @see
         * com.fasterxml.jackson.databind.module.SimpleModule#setupModule
         * (com.fasterxml.jackson.databind.Module.SetupContext)
         */
        @Override
        public void setupModule(final SetupContext context) {
            context.setMixInAnnotations(Exception.class, ThrowableMaskMixin.class);
            context.setMixInAnnotations(TestCustomException.class, ThrowableMaskMixin.class);
        }
    });
    exceptionMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return exceptionMapper;
}