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.jrubygradle.jem.Gem.java

private static ObjectMapper getYamlMapper() {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

From source file:com.evrythng.java.wrapper.util.JSONUtils.java

/**
 * Creates a pre-configured {@link ObjectMapper}.
 *///  w ww.ja v a 2 s  .co  m
public static ObjectMapper createObjectMapper() {

    ObjectMapper mapper = new ObjectMapper();

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setDateFormat(new ISO8601DateFormat());

    mapper.registerModule(new CoreModule());

    return mapper;
}

From source file:com.ubershy.streamsis.project.ProjectSerializator.java

/**
 * Deserialize(load) {@link CuteProject} from file.
 *
 * @param path/*from  w  ww.  j a v a 2  s .c om*/
 *            the path of the file to deserialize
 * @return {@link CuteProject}
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static CuteProject deSerializeFromFile(String path) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
    CuteProject project = null;
    try {
        project = mapper.readValue(Files.readAllBytes(Paths.get(path)), CuteProject.class);
    } catch (JsonGenerationException e) {
        logger.error("CuteProject opening fail: JsonGeneration error");
        e.printStackTrace();
        throw e;
    } catch (JsonMappingException e) {
        logger.error(
                "CuteProject opening fail: mapping error. Can't deserialize Project file. It has different or outdated format "
                        + path);
        e.printStackTrace();
        throw e;
    } catch (IOException e) {
        logger.error("CuteProject opening fail: IO error " + path);
        throw e;
    }
    logger.info("CuteProject opening success");
    return project;
}

From source file:com.tomtom.speedtools.json.JsonObjectMapperFactory.java

public static ObjectMapper createJsonObjectMapper() {

    // Create a Json factory with customer properties.
    final JsonFactory jsonFactory = new JsonFactory();

    // Json parsing features.
    jsonFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
            .configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

    // Json generation features.
    jsonFactory.configure(Feature.QUOTE_FIELD_NAMES, true).configure(Feature.WRITE_NUMBERS_AS_STRINGS, false);

    // Create a custom object mapper from the newly created factory. This object mapper will be used by RestEasy.
    final ObjectMapper mapper = new ObjectMapper(jsonFactory);

    // Set generic mapper configuration.
    mapper.configure(MapperFeature.USE_ANNOTATIONS, true).configure(MapperFeature.AUTO_DETECT_GETTERS, false)
            .configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false)
            .configure(MapperFeature.AUTO_DETECT_SETTERS, false)
            .configure(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);

    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY).setSerializationInclusion(Include.NON_NULL)
            .disableDefaultTyping().disable(SerializationFeature.WRITE_NULL_MAP_VALUES);

    // Set deserialization configuration.
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
            .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);

    // Set serialization configuration.
    mapper.configure(SerializationFeature.INDENT_OUTPUT, false)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true)
            .configure(SerializationFeature.WRAP_ROOT_VALUE, false)
            .configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, false)
            .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
            .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, false);

    // The annotation inspectors and additional mappers should be set by the caller.
    return mapper;
}

From source file:com.buzzcoders.yasw.widgets.map.support.GMapUtils.java

/**
 * Returns the coordinates of the specified address.
 * //from   ww w  . j  a v a2s  .c o  m
 * @param addressText the address to look
 * @return the latitude and longitude information
 */
public static LatLng getAddressCoordinates(String addressText) {
    LatLng coordinates = null;
    GetMethod locateAddressGET = null;
    HttpClient client = null;
    try {
        String addressUrlEncoded = URLEncoder.encode(addressText, "UTF-8");
        String locationFindURL = "http://maps.google.com/maps/api/geocode/json?sensor=false&address="
                + addressUrlEncoded;
        client = new HttpClient();
        locateAddressGET = new GetMethod(locationFindURL);
        int httpRetCode = client.executeMethod(locateAddressGET);
        if (httpRetCode == HttpStatus.SC_OK) {
            String responseBodyAsString = locateAddressGET.getResponseBodyAsString();
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
            JsonNode jsonRoot = mapper.readTree(responseBodyAsString);
            JsonNode location = jsonRoot.path("results").get(0).path("geometry").path("location");
            JsonNode lat = location.get("lat");
            JsonNode lng = location.get("lng");
            coordinates = new LatLng(lat.asDouble(), lng.asDouble());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (locateAddressGET != null)
            locateAddressGET.releaseConnection();
        if (client != null)
            client.getState().clear();
    }
    return coordinates;
}

From source file:com.cloudera.csd.tools.JsonUtil.java

/**
 * Creates a new {@link ObjectMapper} instance with the certain default
 * behavior: (1) sorting properties alphabetically, and (2) support for Joda
 * time format.//from   w ww.j ava2  s. c  o m
 * <p/>
 */
public static ObjectMapper createObjectMapper() {
    ObjectMapper newMapper = new ObjectMapper();
    newMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    newMapper.registerModule(new JodaModule());
    return newMapper;
}

From source file:org.apache.drill.common.logical.JSONOptions.java

private static synchronized ObjectMapper getMapper() {
    if (MAPPER == null) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(Feature.ALLOW_COMMENTS, true);
        MAPPER = mapper;// w w  w  . ja  v  a  2  s  .  c  o m
    }
    return MAPPER;
}

From source file:com.antonjohansson.elasticsearchshell.client.Client.java

private static ObjectMapper getMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

From source file:net.sf.jasperreports.engine.util.JsonUtil.java

public static ObjectMapper createObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    return mapper;
}

From source file:com.hybridbpm.ui.component.chart.util.DiagrammeUtil.java

public static String objectToString(Object object) {
    try {//from  w  ww.  j  a va2  s  . c  o  m
        ObjectMapper mapper = new ObjectMapper();
        //            mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        return mapper.writeValueAsString(object);
    } catch (JsonProcessingException ex) {
        LOG.log(Level.SEVERE, ex.getMessage(), ex);
    }
    return new String();
}