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:net.aethersanctum.lilrest.server.JacksonProvider.java

private ObjectMapper customMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}

From source file:org.apache.commons.scxml2.io.ContentParserTest.java

@Test
public void testParseJson() throws Exception {
    ObjectMapper jsonObjectMapper = new ObjectMapper();
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true);
    // not by default configured, but much easier for unit-testing Java embedded JSON Strings
    jsonObjectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

    ContentParser contentParser = new ContentParser(jsonObjectMapper);

    String jsonObjectString = "{ /*comment*/ 'string' : 'foobar', 'int' : 1, 'boolean' : false, 'null' : null }";
    LinkedHashMap<String, Object> jsonObject = new LinkedHashMap<>();
    jsonObject.put("string", "foobar");
    jsonObject.put("int", new Integer(1));
    jsonObject.put("boolean", Boolean.FALSE);
    jsonObject.put("null", null);
    Assert.assertEquals(jsonObject, contentParser.parseJson(jsonObjectString));

    String jsonArrayString = "[" + jsonObjectString + "," + "# yaml comment\n" + jsonObjectString + "]";
    ArrayList<Object> jsonArray = new ArrayList<>(2);
    jsonArray.add(jsonObject);/*from w w  w .  jav a2 s .c  o m*/
    jsonArray.add(jsonObject);
    Assert.assertEquals(jsonArray, contentParser.parseJson(jsonArrayString));
}

From source file:com.orange.ngsi.ConvertersConfiguration.java

@Bean
public MappingJackson2HttpMessageConverter jsonV1Converter(ObjectMapper objectMapper) {

    // Serialize numbers as strings
    objectMapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);

    // Serialize booleans as strings
    SimpleModule booleanAsString = new SimpleModule("BooleanAsString");
    booleanAsString.addSerializer(Boolean.class, new JsonSerializer<Boolean>() {
        @Override/* w w w . j  a v a2s  . c o  m*/
        public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            jgen.writeString(value.toString());

        }
    });
    objectMapper.registerModule(booleanAsString);

    objectMapper.addMixIn(ContextElement.class, EntityIdMixIn.class);
    objectMapper.addMixIn(AppendContextElementResponse.class, EntityIdMixIn.class);

    return new MappingJackson2HttpMessageConverter(objectMapper);
}

From source file:com.parworks.androidlibrary.response.ARResponseHandlerImpl.java

@Override
public <T> T handleResponse(String contentString, Class<T> typeOfResponse) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    T responseObject = null;/*from  w  w  w.  j  a v a2s  . c o  m*/
    try {
        responseObject = mapper.readValue(contentString, typeOfResponse);
    } catch (JsonParseException e) {
        throw new ARException("Couldn't create the object " + typeOfResponse
                + " because the json was malformed : " + contentString, e);
    } catch (JsonMappingException e) {
        throw new ARException("Mapping the json response to the response object " + typeOfResponse
                + " failed.  Json was : " + contentString, e);
    } catch (IllegalStateException e) {
        throw new ARException("Couldn't convert the json to object " + typeOfResponse
                + " because of illegal state. Json was : " + contentString, e);
    } catch (IOException e) {
        throw new ARException("Couldn't convert the json to object " + typeOfResponse
                + " because of an ioexception. Json was : " + contentString, e);
    }
    return responseObject;
}

From source file:com.parworks.androidlibrary.response.ARResponseHandlerImpl.java

@Override
public <T> T handleResponse(JsonParser jp, Class<T> typeOfResponse) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    T responseObject = null;//  w  ww .j a va2  s .com
    try {
        responseObject = mapper.readValue(jp, typeOfResponse);
    } catch (JsonParseException e) {
        throw new ARException("Couldn't create the object " + typeOfResponse
                + " because the json was malformed : " + jp.toString(), e);
    } catch (JsonMappingException e) {
        throw new ARException("Mapping the json response to the response object " + typeOfResponse
                + " failed.  Json was : " + jp.toString(), e);
    } catch (IllegalStateException e) {
        throw new ARException("Couldn't convert the json to object " + typeOfResponse
                + " because of illegal state. Json was : " + jp.toString(), e);
    } catch (IOException e) {
        throw new ARException("Couldn't convert the json to object " + typeOfResponse
                + " because of an ioexception. Json was : " + jp.toString(), e);
    }
    return responseObject;
}

From source file:org.eluder.coveralls.maven.plugin.logging.JobLogger.java

private ObjectMapper createDefaultJsonMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    return mapper;
}

From source file:ch.thp.proto.spring.time.web.config.SpringMVCRestConfig.java

private ObjectMapper register310TimeModule() {
    ObjectMapper obj = new ObjectMapper();
    obj.registerModule(new JSR310Module());
    obj.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    return obj;/* ww w.  j  a  v a  2 s  .c  o  m*/
}

From source file:org.imsglobal.lti.lti2.ToolConsumerTest.java

@Test
public void TestProfile() throws IOException {
    TestLtiConsumerProfile config = new TestLtiConsumerProfile();
    ToolConsumer consumer = new ToolConsumer("guid", "LTI-2p0", "tcp?", config);
    //consumer.addCapabilites(config.getCapabilities());
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    String json = mapper.writeValueAsString(consumer);
    System.out.println(json);/*from  w  w  w .  java 2 s.co m*/

    ToolConsumer parsed = mapper.readValue(json, ToolConsumer.class);
}

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

@RequestMapping(value = "/rest/customers/{customerId}/addresses", method = RequestMethod.GET)
public String getCustomerAddresses(@PathVariable String customerId) {
    Iterable<AddressEntry> listAddresses = customerEntryRepository.findByIdentifier(customerId).getAddresses();
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    String listAddressesString = "";
    try {/*from  w  ww . jav  a2 s . c o m*/
        listAddressesString = mapper.writeValueAsString(listAddresses);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return listAddressesString;
}

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

/**
 * Transform the json result to a list with T as its member class.
 *
 * @param clazz the type class of transformed list member object
 *//*from   ww  w  . j  av  a 2s  .com*/
public <T> List<T> list(String expression, Class<T> clazz) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return expression == null
            ? mapper.readValue(context,
                    TypeFactory.defaultInstance().constructCollectionType(List.class, clazz))
            : mapper.readValue(JsonPath.compile(expression).read(context).toString(),
                    TypeFactory.defaultInstance().constructCollectionType(List.class, clazz));
}