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

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

Introduction

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

Prototype

public ObjectMapper configure(JsonGenerator.Feature f, boolean state) 

Source Link

Document

Method for changing state of an on/off JsonGenerator feature for JsonFactory instance this object mapper uses.

Usage

From source file:com.oreilly.springdata.rest.client.ClientConfiguration.java

/**
 * Configures the Jackson {@link ObjectMapper} to ignore unknown properties on the client side. E.g.
 * {@link LineItem#getTotal()} causes Jackson to consider {@code total} a property and fails to bind the object as
 * there's no setter accepting a value.//from w w w  .  j a  va2  s. c  o m
 * 
 * @return
 */
@Bean
public RestOperations restOperations() {

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(mapper);
    converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));

    List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
    converters.add(converter);

    RestTemplate template = new RestTemplate();
    template.setMessageConverters(converters);

    return template;
}

From source file:com.mycompany.projetsportmanager.springsecurity.userdetailsservice.MyAuthenticationUserDetailsService.java

/**
 * Load the access key configuration.//from  w w w.  ja va 2s .co  m
 */
@PostConstruct
public void loadAccessKeyConfiguration() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    accessKeyConfiguration = mapper.readValue(accessKeyConfigurationFile.getInputStream(),
            AccessKeyConfiguration.class);
}

From source file:com.xeiam.xchange.mtgox.v1.service.marketdata.streaming.TickerJSONTest.java

@Test
public void testStreamingUnmarshal() throws IOException {

    // Read in the JSON from the example resources
    InputStream is = TickerJSONTest.class
            .getResourceAsStream("/v1/marketdata/streaming/example-ticker-streaming-data.json");

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    Map<String, Object> userInMap = mapper.readValue(is, new TypeReference<Map<String, Object>>() {
    });//from   w w  w .  ja v a  2  s  .co  m

    // Use Jackson to parse it
    mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MtGoxTicker mtGoxTicker = mapper.readValue(mapper.writeValueAsString(userInMap.get("ticker")),
            MtGoxTicker.class);

    // Verify that the example data was unmarshalled correctly
    assertThat(mtGoxTicker.getBuy().getValue()).isEqualTo(new BigDecimal("90.78469"));
    assertThat(mtGoxTicker.getNow()).isEqualTo(1364667533416136L);

}

From source file:guru.nidi.loader.repository.RepositoryLoader.java

private ObjectMapper createMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

From source file:io.helixservice.feature.restservice.marshal.JacksonMarshaller.java

public JacksonMarshaller() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    this.objectMapper = objectMapper;
}

From source file:example.springdata.mongodb.geojson.Application.java

/**
 * Read JSON data from disk and insert those stores.
 * /*from w w  w. j a va2  s.c  om*/
 * @return
 */
public @Bean AbstractRepositoryPopulatorFactoryBean repositoryPopulator() {

    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixIn(GeoJsonPoint.class, GeoJsonPointMixin.class);
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

    Jackson2RepositoryPopulatorFactoryBean factoryBean = new Jackson2RepositoryPopulatorFactoryBean();
    factoryBean.setResources(new Resource[] { new ClassPathResource("starbucks-in-nyc.json") });
    factoryBean.setMapper(mapper);

    return factoryBean;
}

From source file:ar.com.wolox.base.json.DateTimeJsonSerializationTest.java

/** Tests the serialization/deserialization process */
@Test/*w w  w .  j  a v  a  2s.c  o  m*/
public void testSerializationProcess() throws Exception {
    final DateTime date = new DateTime(1988, 11, 29, 12, 0);
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    final SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null))
            .addDeserializer(DateTime.class, new DateTimeJsonDeserializer())
            .addSerializer(DateTime.class, new DateTimeJsonSerializer());
    mapper.registerModule(testModule);

    final String json = mapper.writeValueAsString(new Foo(date));
    final Foo deserialized = mapper.readValue(json, Foo.class);
    assertEquals(date, deserialized.getDate());
}

From source file:com.mapr.synth.samplers.SchemaSampler.java

public SchemaSampler(File input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    init(mapper.<List<FieldSampler>>readValue(input, new TypeReference<List<FieldSampler>>() {
    }));/*from   w w w .j av a2s  . c  o m*/
}

From source file:com.fil.ap.restful.GreetingControllerTests.java

@Test
public void paramGreetingShouldReturnTailoredMessage() throws Exception {

    MvcResult result = this.mockMvc.perform(get("/greeting").param("name", "Spring Community")).andReturn();

    int status = result.getResponse().getStatus();
    byte[] bytes = result.getResponse().getContentAsByteArray();

    String json = new String(bytes, "UTF-8");

    System.out.println(json);/*from  www. j av a  2s .  c o  m*/

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    TypeReference<Greeting> typeRef = new TypeReference<Greeting>() {
    };
    Greeting bean = mapper.readValue(json, typeRef);

    Assert.assertEquals("HTTP Response Status is not 200", 200, status);

    Assert.assertEquals("Content is incorrect", "Hello, Spring Community!", bean.getContent());

    Assert.assertEquals("ID is incorrect", 1, bean.getId());
}

From source file:com.mapr.synth.samplers.SchemaSampler.java

public SchemaSampler(String schemaDefinition) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

    init(mapper.<List<FieldSampler>>readValue(schemaDefinition, new TypeReference<List<FieldSampler>>() {
    }));/*from  w w w. j  a  v  a  2s  .c o  m*/
}