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

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

Introduction

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

Prototype

public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) 

Source Link

Document

Method for setting defalt POJO property inclusion strategy for serialization.

Usage

From source file:org.stem.ClusterManagerDaemon.java

public static JacksonJaxbJsonProvider getJsonProvider() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    //mapper.getSerializationConfig().addMixInAnnotations(File.class, MixIn_File.class);
    JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(mapper,
            JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
    provider.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE);
    provider.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, Boolean.FALSE);
    return provider;
}

From source file:keywhiz.KeywhizService.java

/**
 * Customizes ObjectMapper for common settings.
 *
 * @param objectMapper to be customized/*from  w  w w.  j  av a  2s  .  com*/
 * @return customized input factory
 */
public static ObjectMapper customizeObjectMapper(ObjectMapper objectMapper) {
    objectMapper.registerModules(new Jdk8Module());
    objectMapper.registerModules(new JavaTimeModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}

From source file:io.yields.math.framework.kpi.ExplorerJsonExporter.java

private static ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enableDefaultTyping();/*from  ww w .jav  a 2  s. c  om*/
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.registerModule(new JSR310Module());
    return mapper;
}

From source file:org.hawkular.rx.cdi.JacksonConfig.java

public static void initializeObjectMapper(ObjectMapper mapper) {
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
    mapper.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    mapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
    mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    InventoryJacksonConfig.configure(mapper);
    //need to reconfigure for path serialization
    mapper.addMixIn(CanonicalPath.class, PathSerializationMixin.class);
    mapper.addMixIn(RelativePath.class, PathSerializationMixin.class);
}

From source file:uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser.java

public static ObjectMapper createDefaultMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(SerializationFeature.CLOSE_CLOSEABLE, true);
    mapper.registerModule(getCloseableIterableDeserialiserModule());

    // Use the 'setFilters' method so it is compatible with older versions of jackson
    mapper.setFilters(getFilterProvider());
    return mapper;
}

From source file:org.jongo.marshall.jackson.JacksonProcessor.java

public static ObjectMapper createPreConfiguredMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(AUTO_DETECT_GETTERS, false);
    mapper.configure(AUTO_DETECT_SETTERS, false);
    mapper.setSerializationInclusion(NON_NULL);
    mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(ANY));

    SimpleModule module = new SimpleModule("jongo", new Version(1, 0, 0, null, null, null));
    addBSONTypeSerializers(module);/*w  ww.  j a  va 2 s. c  o  m*/
    mapper.registerModule(module);
    return mapper;
}

From source file:org.mango.marshall.jackson.JacksonProcessor.java

public static ObjectMapper createPreConfiguredMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(AUTO_DETECT_GETTERS, false);
    mapper.configure(AUTO_DETECT_SETTERS, false);
    mapper.setSerializationInclusion(NON_NULL);
    mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(ANY));

    SimpleModule module = new SimpleModule("mango", new Version(1, 0, 0, null, null, null));
    addBSONTypeSerializers(module);//from w  w  w.jav  a2 s .  co  m
    mapper.registerModule(module);
    return mapper;
}

From source file:com.unboundid.scim2.common.utils.MapperFactory.java

/**
 * Creates a custom SCIM compatible Jackson ObjectMapper. Creating new
 * ObjectMapper instances are expensive so instances should be shared if
 * possible. This can be used to set the factory used to build new instances
 * of the object mapper used by the SCIM 2 SDK.
 *
 * @return an Object Mapper with the correct options set for serializing
 *     and deserializing SCIM JSON objects.
 *///w w w.ja v a 2s  . c  om
public static ObjectMapper createObjectMapper() {
    ObjectMapper mapper = new ObjectMapper(new ScimJsonFactory());

    // Don't serialize POJO nulls as JSON nulls.
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

    // Only use ISO8601 format for dates.
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.setDateFormat(new ScimDateFormat());

    // Do not care about case when de-serializing POJOs.
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

    // Use the case-insensitive JsonNodes.
    mapper.setNodeFactory(new ScimJsonNodeFactory());

    for (DeserializationFeature feature : deserializationCustomFeatures.keySet()) {
        mapper.configure(feature, deserializationCustomFeatures.get(feature));
    }

    for (JsonGenerator.Feature feature : jsonGeneratorCustomFeatures.keySet()) {
        mapper.configure(feature, jsonGeneratorCustomFeatures.get(feature));
    }

    for (JsonParser.Feature feature : jsonParserCustomFeatures.keySet()) {
        mapper.configure(feature, jsonParserCustomFeatures.get(feature));
    }

    for (MapperFeature feature : mapperCustomFeatures.keySet()) {
        mapper.configure(feature, mapperCustomFeatures.get(feature));
    }

    for (SerializationFeature feature : serializationCustomFeatures.keySet()) {
        mapper.configure(feature, serializationCustomFeatures.get(feature));
    }

    return mapper;
}

From source file:de.javagl.jgltf.model.io.GltfUtils.java

/**
 * Creates a deep copy of the given {@link GlTF}.<br>
 * <br>//from   w ww .  ja va 2  s . c o  m
 * Note: Some details about the copy are not specified. E.g. whether
 * values that are mapped to <code>null</code> are still contained
 * in the copy. The goal of this method is to create a copy that is,
 * as far as reasonably possible, "structurally equivalent" to the
 * given input.
 * 
 * @param gltf The input 
 * @return The copy
 * @throws GltfException If the copy can not be created
 */
static GlTF copy(GlTF gltf) {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(Include.NON_NULL);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        objectMapper.writeValue(baos, gltf);
        return objectMapper.readValue(baos.toByteArray(), GlTF.class);
    } catch (IOException e) {
        throw new GltfException("Could not copy glTF", e);
    }
}

From source file:com.netflix.spinnaker.halyard.core.GlobalApplicationOptions.java

public static GlobalApplicationOptions getInstance() {
    if (GlobalApplicationOptions.options == null) {
        Yaml yamlParser = new Yaml(new SafeConstructor());
        ObjectMapper objectMapper = new ObjectMapper();

        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        try {// ww w .j  av a 2s  .c o  m
            GlobalApplicationOptions.options = objectMapper.convertValue(
                    yamlParser.load(FileUtils.openInputStream(new File(CONFIG_PATH))),
                    GlobalApplicationOptions.class);
        } catch (IOException e) {
            GlobalApplicationOptions.options = new GlobalApplicationOptions();
        }
    }
    return GlobalApplicationOptions.options;
}