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.hateoas.hal.ResourceMappingJackson2HttpMessageConverter.java

/**
 * Construct a new {@code ResourceMappingJackson2HttpMessageConverter} with a
 * customized {@link ObjectMapper} to support HAL resources.
 *//*w ww  .  j a  v  a2  s.c om*/
public ResourceMappingJackson2HttpMessageConverter() {
    super(Resource.class);
    super.setSupportedMediaTypes(
            Collections.singletonList(new MediaType("application", "hal+json", DEFAULT_CHARSET)));
    ObjectMapper objectMapper = getObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    super.setObjectMapper(objectMapper);
}

From source file:org.elasticstore.server.config.ElasticstoreConfiguration.java

@Bean
@Primary/*  w w w . j a  v a  2s  .  c  om*/
public ObjectMapper objectMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JSR310Module());
    return mapper;
}

From source file:org.springframework.social.facebook.api.impl.json.QuestionOptionListDeserializer.java

@SuppressWarnings("unchecked")
@Override//  w ww.j  a v a2s . co m
public List<QuestionOption> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new FacebookModule());
    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        TreeNode dataNode = jp.readValueAs(JsonNode.class).get("data");
        if (dataNode != null) {
            // TODO: THIS PROBABLY ISN"T RIGHT
            return (List<QuestionOption>) mapper.reader(new TypeReference<List<QuestionOption>>() {
            }).readValue((JsonNode) dataNode);
        }
    }

    return null;
}

From source file:com.ikanow.aleph2.data_model.utils.BeanTemplateUtils.java

/** Configures a mapper with the desired properties for use in Aleph2
 * @param configure_me - leave this empty to create a new mapper, or add one to configure an existing mapper
 * @return//from  w w w  .j a v  a 2s  .  c  o  m
 */
public static ObjectMapper configureMapper(final Optional<ObjectMapper> configure_me) {
    final ObjectMapper mapper = configure_me.orElse(new ObjectMapper());
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

    final SimpleModule module = new SimpleModule();
    module.addDeserializer(Number.class, new NumberDeserializer());
    mapper.registerModule(module);

    return mapper;
}

From source file:org.ambientlight.RestConfig.java

public RestConfig() {
    packages("org.ambientlight.webservice");

    register(new ContextResolver<ObjectMapper>() {

        @Override/*from   www. java 2 s.c  om*/
        public ObjectMapper getContext(Class<?> type) {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.registerModule(new EntityIdModule());
            return objectMapper;
        }
    });
}

From source file:org.jddd.jackson.SimpleValueObjectSerializerModifierUnitTests.java

@Test
public void automaticallyUnwrapsSinglePropertyValueObjects() throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new DddModule());

    EmailAddress email = EmailAddress.of("foo@bar.com");
    String result = mapper.writeValueAsString(new Wrapper(email));

    System.out.println(result);//from w  ww. j a  v a2  s.com

    assertThat(JsonPath.<String>read(result, "$.email")).isEqualTo("foo@bar.com");
}

From source file:org.mayocat.shop.payment.store.jdbi.mapper.GatewayCustomerDataMapper.java

@Override
public GatewayCustomerData map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
    UUID customerId = (UUID) resultSet.getObject("customer_id");
    String gateway = resultSet.getString("gateway");

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new GuavaModule());
    try {//ww w  .  j  av  a2 s. com
        Map<String, Object> data = mapper.readValue(resultSet.getString("customer_data"),
                new TypeReference<Map<String, Object>>() {
                });

        return new GatewayCustomerData(customerId, gateway, data);
    } catch (IOException e) {
        final Logger logger = LoggerFactory.getLogger(GatewayCustomerDataMapper.class);
        logger.error("Failed to de-serialize gateway customer data", e);

        return null;
    }
}

From source file:org.springframework.social.facebook.api.impl.json.MessageTagMapDeserializer.java

@SuppressWarnings("unchecked")
@Override/*  w w w .j a  va2 s  .  c o  m*/
public Map<Integer, List<MessageTag>> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new FacebookModule());
    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        JsonNode dataNode = jp.readValueAs(JsonNode.class);
        if (dataNode != null) {
            return (Map<Integer, List<MessageTag>>) mapper
                    .reader(new TypeReference<Map<Integer, List<MessageTag>>>() {
                    }).readValue(dataNode);
        }
    }

    return Collections.emptyMap();
}

From source file:org.springframework.social.facebook.api.impl.json.StoryTagMapDeserializer.java

@SuppressWarnings("unchecked")
@Override/*from  w  w  w  .  j a  va2 s  .c o m*/
public Map<Integer, List<StoryTag>> deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new FacebookModule());
    jp.setCodec(mapper);
    if (jp.hasCurrentToken()) {
        JsonNode dataNode = jp.readValueAs(JsonNode.class);
        if (dataNode != null) {
            return (Map<Integer, List<StoryTag>>) mapper
                    .reader(new TypeReference<Map<Integer, List<StoryTag>>>() {
                    }).readValue(dataNode);
        }
    }

    return Collections.emptyMap();
}

From source file:org.mayocat.shop.payment.store.jdbi.mapper.GatewayTenantDataMapper.java

@Override
public GatewayTenantData map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
    UUID tenantId = (UUID) resultSet.getObject("tenant_id");
    String gateway = resultSet.getString("gateway");

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new GuavaModule());
    try {/*from ww  w. j  a  va2  s .  c om*/
        Map<String, Object> data = mapper.readValue(resultSet.getString("tenant_data"),
                new TypeReference<Map<String, Object>>() {
                });

        return new GatewayTenantData(tenantId, gateway, data);
    } catch (IOException e) {
        final Logger logger = LoggerFactory.getLogger(GatewayTenantDataMapper.class);
        logger.error("Failed to de-serialize gateway tenant data", e);

        return null;
    }
}