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.github.yongchristophertang.engine.java.handler.JsonTransformer.java

/**
 * Transform the json result to a map with K as its key class and V as its value class.
 *
 * @param keyClass   the type class of transformed map key object
 * @param valueClass the type class of transformed map value object
 *//*from w w w.jav a2s  .com*/
public <K, V> Map<K, V> map(String expression, Class<K> keyClass, Class<V> valueClass) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return expression == null
            ? mapper.readValue(context,
                    TypeFactory.defaultInstance().constructMapLikeType(Map.class, keyClass, valueClass))
            : mapper.readValue(JsonPath.compile(expression).read(context).toString(),
                    TypeFactory.defaultInstance().constructMapLikeType(Map.class, keyClass, valueClass));
}

From source file:org.jrb.docasm.ApplicationConfig.java

@Bean
public MappingJackson2HttpMessageConverter messageConverter() {

    // assemble json mapper
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setDateFormat(DateFormat.getDateInstance());
    objectMapper.configure(Feature.WRITE_NUMBERS_AS_STRINGS, true);

    // assemble json message converter
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setObjectMapper(objectMapper);

    return converter;
}

From source file:cn.designthoughts.sample.axon.sfav.query.customer.controller.QueryCustomerController.java

@RequestMapping(value = "/rest/customers", method = RequestMethod.GET)
public String listCustomer() {
    Iterable<CustomerEntry> listCustomers = customerEntryRepository.findAll();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    String listCustomerString = "";
    try {/*from w w w . jav a  2  s. c o m*/
        listCustomerString = mapper.writeValueAsString(listCustomers);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    return listCustomerString;
}

From source file:cn.designthoughts.sample.axon.sfav.query.customer.controller.QueryCustomerController.java

@RequestMapping(value = "/rest/customers/{customerId}", method = RequestMethod.GET)
public String getCustomer(@PathVariable String customerId) {
    CustomerEntry customer = customerEntryRepository.findByIdentifier(customerId);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    String customerString = "";
    try {/*from   ww w  . j  a  v a2 s . co  m*/
        customerString = mapper.writeValueAsString(customer);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    return customerString;
}

From source file:com.github.yongchristophertang.engine.java.handler.JsonTransformer.java

/**
 * Transform the json result to an instance of T.
 *
 * @param clazz the type class of transformed object
 *///from   w ww  . j av a  2 s. co  m
public <T> T object(String expression, Class<T> clazz) {
    Objects.nonNull(expression);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    try {
        return expression == null ? mapper.readValue(context, clazz)
                : mapper.readValue(JsonPath.compile(expression).read(context).toString(), clazz);
    } catch (Exception e) {
        return clazz.cast(JsonPath.compile(expression).read(context));
    }
}

From source file:com.sra.biotech.submittool.persistence.client.RestClientConfiguration.java

public MappingJackson2HttpMessageConverter halConverter() {
    RelProvider defaultRelProvider = defaultRelProvider();
    RelProvider annotationRelProvider = annotationRelProvider();

    OrderAwarePluginRegistry<RelProvider, Class<?>> relProviderPluginRegistry = OrderAwarePluginRegistry
            .create(Arrays.asList(defaultRelProvider, annotationRelProvider));

    DelegatingRelProvider delegatingRelProvider = new DelegatingRelProvider(relProviderPluginRegistry);

    ObjectMapper halObjectMapper = new ObjectMapper();
    halObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    halObjectMapper.registerModule(new Jackson2HalModule());
    halObjectMapper//from   w  ww.  j  a v a  2 s . c o m
            .setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(delegatingRelProvider, null));

    MappingJackson2HttpMessageConverter halConverter = new MappingJackson2HttpMessageConverter();
    halConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
    halConverter.setObjectMapper(halObjectMapper);
    return halConverter;
}

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

@Test
public void testUnmarshal() throws IOException {

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

    // Use Jackson to parse it
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    MtGoxTicker mtGoxTicker = mapper.readValue(is, MtGoxTicker.class);

    // Verify that the example data was unmarshalled correctly
    assertThat("Unexpected Return Buy value", mtGoxTicker.getBuy().getValue().doubleValue(), equalTo(4.89002));
    assertThat("Unexpected Return Last value", mtGoxTicker.getLast().getValue().doubleValue(),
            equalTo(4.89000));/*from  w  w w .j  a  va  2 s  .  c o  m*/
    assertThat("Unexpected Return Bid value", mtGoxTicker.getBuy().getValue().doubleValue(), equalTo(4.89002));
    assertThat("Unexpected Return Ask value", mtGoxTicker.getSell().getValue().doubleValue(), equalTo(4.91227));
    assertThat("Unexpected Return High value", mtGoxTicker.getHigh().getValue().doubleValue(),
            equalTo(4.98000));
    assertThat("Unexpected Return Low value", mtGoxTicker.getLow().getValue().doubleValue(), equalTo(4.84272));
    assertThat("Unexpected Return Volume value", mtGoxTicker.getVol().getValue().doubleValue(),
            equalTo(57759.66891627));
}

From source file:org.apache.calcite.adapter.elasticsearch.ElasticsearchSchemaFactory.java

@Override
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {

    final Map map = (Map) operand;

    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

    try {/*from   w w  w .j  ava  2s .c  o  m*/
        final Map<String, Integer> coordinates = mapper.readValue((String) map.get("coordinates"),
                new TypeReference<Map<String, Integer>>() {
                });

        final RestClient client = connect(coordinates);

        final Map<String, String> userConfig = mapper.readValue((String) map.get("userConfig"),
                new TypeReference<Map<String, String>>() {
                });

        final String index = (String) map.get("index");
        Preconditions.checkArgument(index != null, "index is missing in configuration");
        return new ElasticsearchSchema(client, new ObjectMapper(), index);
    } catch (IOException e) {
        throw new RuntimeException("Cannot parse values from json", e);
    }
}

From source file:io.neocdtv.jarxrs.server.ResourceExample.java

@GET
@Path("schema-jackson")
public Response generateSchemaJackson() throws JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);

    final com.fasterxml.jackson.databind.jsonschema.JsonSchema generateJsonSchema = mapper
            .generateJsonSchema(Object1.class);

    return Response.ok(mapper.writeValueAsString(generateJsonSchema)).build();
}

From source file:com.seyren.core.service.notification.PagerDutyNotificationService.java

private Map<String, String> details(Check check, List<Alert> alerts) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
    mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy());
    return ImmutableMap.<String, String>builder().put("CHECK", mapper.writeValueAsString(check))
            .put("STATE", check.getState().name()).put("ALERTS", mapper.writeValueAsString(alerts))
            .put("SEYREN_URL", seyrenConfig.getBaseUrl()).build();
}