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:ch.webmate.api.doclet.reporter.swagger2.Swagger2Reporter.java

protected ObjectMapper createObjectMapper() {

    ObjectMapper objectMapper = new ObjectMapper();

    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    return objectMapper;

}

From source file:org.lable.rfc3881.auditlogger.serialization.ReferenceableSerializerTest.java

@Test
public void nullFieldTest() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    objectMapper.registerModule(new ReferenceableSerializerModule());

    CodeReference codeReference = new CodeReference("CS", "001", "One");

    String result = objectMapper.writeValueAsString(codeReference);

    assertThat(result, is("{" + "\"cs\":\"CS\"," + "\"code\":\"001\"," + "\"dn\":\"One\"" + "}"));
}

From source file:org.lable.rfc3881.auditlogger.serialization.ReferenceableSerializerTest.java

@Test
public void nullFieldAlwaysTest() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    objectMapper.registerModule(new ReferenceableSerializerModule());

    CodeReference codeReference = new CodeReference("CS", "", "001", "One", null);

    String result = objectMapper.writeValueAsString(codeReference);

    assertThat(result, is("{" + "\"cs\":\"CS\"," + "\"code\":\"001\"," + "\"csn\":\"\"," + "\"dn\":\"One\","
            + "\"ot\":null" + "}"));
}

From source file:org.lable.rfc3881.auditlogger.serialization.ReferenceableSerializerTest.java

@Test
public void nullFieldAllowEmptyTest() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.registerModule(new ReferenceableSerializerModule());

    CodeReference codeReference = new CodeReference("CS", "", "001", "One");

    String result = objectMapper.writeValueAsString(codeReference);

    assertThat(result,/*www. j a  v a2s  .c  o m*/
            is("{" + "\"cs\":\"CS\"," + "\"code\":\"001\"," + "\"csn\":\"\"," + "\"dn\":\"One\"" + "}"));
}

From source file:yamlparse.parser.BioboxfileOutparser.java

/**
 * Parsing a ParseableType into a file described by an outputPath
 * @param outputPath is the path where to parse to - please check validity
 * by yourself//from w  w  w .j  a v a2s . c  o  m
 * @param abstractTop should be a BioboxTopType
 */
@Override
public void parse(String outputPath, ParseableType abstractTop) {
    File localFile = new File(outputPath);
    BioboxTopType bbxType = (BioboxTopType) abstractTop;
    try {
        if (!localFile.exists()) {
            localFile.createNewFile();
        }
        this.path = localFile.getAbsolutePath();

        YAMLFactory factory = new YAMLFactory();
        ObjectMapper yamlmap = new ObjectMapper(factory);
        yamlmap.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        FileOutputStream fos = new FileOutputStream(localFile);

        factory.createGenerator(fos).writeObject(bbxType);
    } catch (IOException ex) {
        Logger.getLogger(BioboxfileOutparser.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:de.escalon.hypermedia.spring.HydraMessageConverter.java

public HydraMessageConverter() {
    ObjectMapper objectMapper = new ObjectMapper();
    // see https://github.com/json-ld/json-ld.org/issues/76
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    SimpleModule module = new JacksonHydraModule();
    objectMapper.registerModule(module);
    this.setObjectMapper(objectMapper);
    this.setSupportedMediaTypes(Arrays.asList(HypermediaTypes.APPLICATION_JSONLD));
}

From source file:com.amazon.feeds.SampleFeedGenerator.java

/**
 * The method for generating sample feeds.
 *
 * @param format The class containing the format specifications.
 * @param items The number of items to generate.
 * @param ext File extension.//from w  w w .  j av  a 2  s .  com
 */
public void createSampleFeed(IFeedFormat format, int items, String ext) throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    mapper.getFactory().setCharacterEscapes(format.getEscapeRules());

    // create output file
    String out = format.getFeedFormat() + "-" + items + "." + ext;
    // TODO: add XML support

    File outFile = new File(SAMPLE_PATH, out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }

    // populate sample feed
    System.out.println("Generating " + items + (items == 1 ? " item" : " items") + " for "
            + format.getProvider() + " feed at " + outFile.getAbsolutePath());
    format.populate(items);

    // write JSON to file
    if (format.usePrettyPrint()) {

        DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("   ", DefaultIndenter.SYS_LF);
        DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
        printer.indentObjectsWith(indenter);
        printer.indentArraysWith(indenter);
        mapper.writer(printer).writeValue(outFile, format);
    } else {
        mapper.writeValue(outFile, format);
    }
}

From source file:com.samovich.service.blueprint.App.java

/**
 * Object mapper//from w w  w .j a  v a2s .c  o  m
 * @return mapper
 */
@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new AfterburnerModule());
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.registerModule(new JavaTimeModule());
    return mapper;
}

From source file:org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter.java

/**
 * Creates a new {@link AlpsJsonHttpMessageConverter} for the given {@link Converter}.
 * /*from  w  w  w. ja  v  a  2  s. c o m*/
 * @param converter must not be {@literal null}.
 */
public AlpsJsonHttpMessageConverter(RootResourceInformationToAlpsDescriptorConverter converter) {

    Assert.notNull(converter, "Converter must not be null!");

    this.converter = converter;

    ObjectMapper mapper = getObjectMapper();
    mapper.setSerializationInclusion(Include.NON_EMPTY);

    setPrettyPrint(true);
    setSupportedMediaTypes(Arrays.asList(RestMediaTypes.ALPS_JSON, MediaType.APPLICATION_JSON, MediaType.ALL));
}

From source file:com.foresee.date.AcsApiDateRange.java

public String ToAcsApiJson() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from w ww  .j av  a  2 s.co m*/
        mapper.writeValue(baos, this);
    } catch (Exception exc) {
        _logger.error("Failed to serialize object to json", exc);
        return null;
    }
    return baos.toString();
}