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: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);//ww w .ja va2  s .  com
    return mapper;
}

From source file:com.groupdocs.sdk.common.ApiInvoker.java

public static String serialize(Object obj) throws ApiException {
    try {/*from   w  ww.j a  v a2s  . c  o m*/
        if (obj != null) {
            ObjectMapper jsonMapper = JsonUtil.getJsonMapper();
            jsonMapper.setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL);
            return jsonMapper.writeValueAsString(obj);
        } else
            return "";
    } catch (Exception e) {
        throw new ApiException(500, e.getMessage());
    }
}

From source file:com.googlecode.wickedcharts.highcharts.jackson.JsonRenderer.java

private static ObjectMapper createDefaultObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.setSerializationInclusion(Include.NON_NULL);
    return mapper;
}

From source file:org.apache.nifi.toolkit.s2s.SiteToSiteCliMain.java

/**
 * Prints the usage to System.out/*w  ww  .  ja v a  2  s . c om*/
 *
 * @param errorMessage optional error message
 * @param options      the options object to print usage for
 */
public static void printUsage(String errorMessage, Options options) {
    if (errorMessage != null) {
        System.out.println(errorMessage);
        System.out.println();
        System.out.println();
    }
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    System.out.println(
            "s2s is a command line tool that can either read a list of DataPackets from stdin to send over site-to-site or write the received DataPackets to stdout");
    System.out.println();
    System.out.println(
            "The s2s cli input/output format is a JSON list of DataPackets.  They can have the following formats:");
    try {
        System.out.println();
        objectMapper.writeValue(System.out, Arrays.asList(
                new DataPacketDto("hello nifi".getBytes(StandardCharsets.UTF_8)).putAttribute("key", "value")));
        System.out.println();
        System.out.println(
                "Where data is the base64 encoded value of the FlowFile content (always used for received data) or");
        System.out.println();
        objectMapper.writeValue(System.out,
                Arrays.asList(new DataPacketDto(new HashMap<>(), new File("EXAMPLE").getAbsolutePath())
                        .putAttribute("key", "value")));
        System.out.println();
        System.out.println("Where dataFile is a file to read the FlowFile content from");
        System.out.println();
        System.out.println();
        System.out.println(
                "Example usage to send a FlowFile with the contents of \"hey nifi\" to a local unsecured NiFi over http with an input port named input:");
        System.out.print("echo '");
        DataPacketDto dataPacketDto = new DataPacketDto("hey nifi".getBytes(StandardCharsets.UTF_8));
        dataPacketDto.setAttributes(null);
        objectMapper.writeValue(System.out, Arrays.asList(dataPacketDto));
        System.out.println("' | bin/s2s.sh -n input -p http");
        System.out.println();
    } catch (IOException e) {
        e.printStackTrace();
    }
    HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.setWidth(160);
    helpFormatter.printHelp("s2s", options);
    System.out.flush();
}

From source file:org.restdoc.api.util.RestDocParser.java

/**
 * @return the Jackson mapper for RestDoc
 *///from   w  w w.  java 2  s .c o m
public static ObjectMapper createMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    // Allow comments in JSON
    mapper.configure(Feature.ALLOW_COMMENTS, true);
    // Allow unknown properties for extensibility
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    // Include only non-null values
    mapper.setSerializationInclusion(Include.NON_NULL);
    return mapper;
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.HttpRequestFactory.java

private static String toJsonString(Object objectToConvert) {
    try {/*from  w  w w. ja v  a  2  s .co  m*/
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        return objectMapper.writeValueAsString(objectToConvert);
    } catch (JsonProcessingException e) {
        throw new RuntimeException("Error while converting object to JSON", e);
    }
}

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

/**
 * Creates a pre-configured {@link ObjectMapper}.
 *///from  w  w  w.  ja  v  a2 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:org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService.java

private static OvsdbClient getChannelClient(Channel channel, ConnectionType type,
        ExecutorService executorService) {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setSerializationInclusion(Include.NON_NULL);

    JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
    JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
    binderHandler.setContext(channel);//from w w  w.j  av a  2s  .  c o m
    channel.pipeline().addLast(binderHandler);

    OvsdbRPC rpc = factory.getClient(channel, OvsdbRPC.class);
    OvsdbClientImpl client = new OvsdbClientImpl(rpc, channel, type, executorService);
    connections.put(client, channel);
    ChannelFuture closeFuture = channel.closeFuture();
    closeFuture.addListener(new ChannelConnectionHandler(client));
    return client;
}

From source file:com.wrmsr.kleist.util.Json.java

public static ObjectMapper newObjectMapper() {
    // TODO: io.airlift.json
    ObjectMapper objectMapper = new ObjectMapper();

    // ignore unknown fields (for backwards compatibility)
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    // use ISO dates
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    // skip fields that are null instead of writing an explicit json null value
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

    // disable auto detection of json properties... all properties must be explicit
    objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_FIELDS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
    objectMapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
    objectMapper.disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS);
    objectMapper.disable(MapperFeature.INFER_PROPERTY_MUTATORS);
    objectMapper.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS);

    objectMapper.registerModules(new Jdk8Module(), new JSR310Module(), new GuavaModule());

    return objectMapper;
}

From source file:com.yahoo.gondola.container.Utils.java

public static ObjectMapper getObjectMapperInstance() {
    if (objectMapper == null) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
        mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        objectMapper = mapper;//from   w w w  . j a  va2s.  co  m
    }
    return objectMapper;
}