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:nebula.plugin.metrics.dispatcher.AbstractMetricsDispatcher.java

@VisibleForTesting
public static ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JodaModule());
    registerEnumModule(mapper);//from   www  .  j a v a2 s.c o  m
    return mapper;
}

From source file:je.backit.rest.JacksonContextResolver.java

private static ObjectMapper init() {
    ObjectMapper om = new ObjectMapper();
    om.registerModule(new JSR310Module());
    om.registerModule(new JooqModule());
    om.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    om.getFactory().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false)
            .configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false);
    om.configure(WRITE_DATES_AS_TIMESTAMPS, false);
    om.setVisibilityChecker(om.getSerializationConfig().getDefaultVisibilityChecker()
            .withIsGetterVisibility(NONE).withGetterVisibility(NONE).withFieldVisibility(ANY));
    return om;//ww  w  .  jav a2 s.co m
}

From source file:org.tynamo.resteasy.modules.SwaggerModule.java

@Contribute(javax.ws.rs.core.Application.class)
public static void jacksonJsonProviderSetup(Configuration<Object> singletons) {

    final ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    /**//from  w w  w  .j av  a 2s  .c om
     * "publishedDate": 1384267338786,
     * vs
     * "publishedDate": "2013-11-12T14:42:18.786+0000",
     */
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    JacksonJaxbJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider();
    jacksonJsonProvider.setMapper(mapper);

    singletons.add(jacksonJsonProvider);
}

From source file:org.springframework.hateoas.client.Traverson.java

/**
 * Creates a new {@link HttpMessageConverter} to support HAL.
 * //from   w w w .  j  a  v a  2 s  .c  o  m
 * @return
 */
private static final HttpMessageConverter<?> getHalConverter() {

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

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

    converter.setObjectMapper(mapper);
    converter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));

    return converter;
}

From source file:org.hawkular.bus.common.AbstractMessage.java

/**
 * Convenience static method that converts a JSON string to a particular message object.
 *
 * @param json the JSON string//from ww w  . ja v  a 2 s  .c  o  m
 * @param clazz the class whose instance is represented by the JSON string
 *
 * @return the message object that was represented by the JSON string
 */
public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) {
    try {
        Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz);

        final ObjectMapper mapper = (ObjectMapper) buildObjectMapperForDeserializationMethod.invoke(null);
        if (FailOnUnknownProperties.class.isAssignableFrom(clazz)) {
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
        }

        return mapper.readValue(json, clazz);
    } catch (Exception e) {
        throw new IllegalStateException("JSON message cannot be converted to object of type [" + clazz + "]",
                e);
    }
}

From source file:com.groupon.jackson.ObjectMapperFactory.java

private static ObjectMapper createModifiableObjectMapper(final String name, final ObjectMapper objectMapper) {
    final SimpleModule module = new SimpleModule(name);
    objectMapper.registerModule(module);
    objectMapper.registerModule(new GuavaModule());
    objectMapper.registerModule(new Jdk7Module());
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(DeserializationFeature.WRAP_EXCEPTIONS, false);
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setDateFormat(new ISO8601DateFormat());
    return objectMapper;
}

From source file:org.hawkular.bus.common.AbstractMessage.java

/**
 * Convenience static method that reads a JSON string from the given stream and converts the JSON
 * string to a particular message object. The input stream will remain open so the caller can
 * stream any extra data that might appear after it.
 *
 * Because of the way the JSON parser works, some extra data might have been read from the stream
 * that wasn't part of the JSON message but was part of the extra data that came with it. Because of this,
 * the caller should no longer use the given stream but instead read the extra data via the returned
 * object (see {@link BasicMessageWithExtraData#getBinaryData()}) since it will handle this condition
 * properly.//from   w w w. ja va  2 s . co  m
 *
 * @param in input stream that has a JSON string at the head.
 * @param clazz the class whose instance is represented by the JSON string
 *
 * @return a POJO that contains a message object that was represented by the JSON string found
 *         in the stream. This returned POJO will also contain a {@link BinaryData} object that you
 *         can use to stream any additional data that came in the given input stream.
 */
public static <T extends BasicMessage> BasicMessageWithExtraData<T> fromJSON(InputStream in, Class<T> clazz) {
    final T obj;
    final byte[] remainder;
    try (JsonParser parser = new JsonFactory().configure(Feature.AUTO_CLOSE_SOURCE, false).createParser(in)) {
        Method buildObjectMapperForDeserializationMethod = findBuildObjectMapperForDeserializationMethod(clazz);

        final ObjectMapper mapper = (ObjectMapper) buildObjectMapperForDeserializationMethod.invoke(null);
        if (FailOnUnknownProperties.class.isAssignableFrom(clazz)) {
            mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
        }

        obj = mapper.readValue(parser, clazz);
        final ByteArrayOutputStream remainderStream = new ByteArrayOutputStream();
        final int released = parser.releaseBuffered(remainderStream);
        remainder = (released > 0) ? remainderStream.toByteArray() : new byte[0];
    } catch (Exception e) {
        throw new IllegalArgumentException("Stream cannot be converted to JSON object of type [" + clazz + "]",
                e);
    }
    return new BasicMessageWithExtraData<T>(obj, new BinaryData(remainder, in));
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java

public static ConfigurationDto getConfiguration(String configString) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    return mapper.readValue(configString, ConfigurationDto.class);
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java

public static VisibilitySettingsDto getVisibilitySettings(String visibilitySettings) throws ServiceException {
    if (visibilitySettings == null) {
        return null;
    }/*from   w  w w .  j a  v a2  s .co  m*/
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return mapper.readValue(visibilitySettings, VisibilitySettingsDto.class);
    } catch (IOException e) {
        throw new ServiceException("Parse Exception from Json to Object", e);
    }
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.util.MapConfigHelper.java

public static StyleSettingsDto getStyleSettings(String styleSettings) throws ServiceException {
    if (styleSettings == null) {
        return null;
    }/*from w  w w .ja  va 2 s.c o m*/
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return mapper.readValue(styleSettings, StyleSettingsDto.class);
    } catch (IOException e) {
        throw new ServiceException("Parse Exception from Json to Object", e);
    }
}