Example usage for com.fasterxml.jackson.annotation PropertyAccessor GETTER

List of usage examples for com.fasterxml.jackson.annotation PropertyAccessor GETTER

Introduction

In this page you can find the example usage for com.fasterxml.jackson.annotation PropertyAccessor GETTER.

Prototype

PropertyAccessor GETTER

To view the source code for com.fasterxml.jackson.annotation PropertyAccessor GETTER.

Click Source Link

Document

Getters are methods used to get a POJO field value for serialization, or, under certain conditions also for de-serialization.

Usage

From source file:it.bz.tis.integreen.carsharingbzit.api.ApiClient.java

public <T> T callWebService(ServiceRequest request, Class<T> clazz) throws IOException {

    request.request.technicalUser.username = this.user;
    request.request.technicalUser.password = this.password;

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE)
            .setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY)
            .setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY)
            .setVisibility(PropertyAccessor.SETTER, Visibility.PUBLIC_ONLY);
    mapper.enable(SerializationFeature.INDENT_OUTPUT);

    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, request);//from   www .  ja  v  a 2 s .  c om

    String requestJson = sw.getBuffer().toString();

    logger.debug("callWebService(): jsonRequest:" + requestJson);

    URL url = new URL(this.endpoint);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    OutputStream out = conn.getOutputStream();
    out.write(requestJson.getBytes("UTF-8"));
    out.flush();
    int responseCode = conn.getResponseCode();

    InputStream input = conn.getInputStream();

    ByteArrayOutputStream data = new ByteArrayOutputStream();
    int len;
    byte[] buf = new byte[50000];
    while ((len = input.read(buf)) > 0) {
        data.write(buf, 0, len);
    }
    conn.disconnect();
    String jsonResponse = new String(data.toByteArray(), "UTF-8");

    if (responseCode != 200) {
        throw new IOException(jsonResponse);
    }

    logger.debug("callWebService(): jsonResponse:" + jsonResponse);

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    T response = mapper.readValue(new StringReader(jsonResponse), clazz);

    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    sw = new StringWriter();
    mapper.writeValue(sw, response);
    logger.debug(
            "callWebService(): parsed response into " + response.getClass().getName() + ":" + sw.toString());

    return response;
}

From source file:org.pac4j.vertx.DefaultEventBusObjectConverter.java

public DefaultEventBusObjectConverter() {
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

    addMixIn(BearerAccessToken.class, BearerAccessTokenMixin.class);
    addMixIn(Scope.Value.class, ValueMixin.class);
    addMixIn(Token.class, TokenMixin.class);
}

From source file:com.tectonica.thirdparty.Jackson2.java

public static ObjectMapper createPropsMapper() {
    ObjectMapper mapper = new ObjectMapper();

    // limit to props only
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.ANY);

    // general configuration
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

    return mapper;
}

From source file:com.codenvy.cli.preferences.file.JsonPreferences.java

@JsonCreator
protected JsonPreferences(Map<String, Object> innerPreferences) {
    this.callbackList = new ArrayList<>();
    this.innerPreferences = new ConcurrentHashMap<String, Object>(innerPreferences);

    this.mapper = new ObjectMapper();
    this.mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    this.mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
    this.mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    this.mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY);
    this.mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY);

    this.mapType = this.mapper.getTypeFactory().constructMapType(ConcurrentMap.class, String.class,
            Object.class);
}

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  ww .  j  a  va2s  . c  o  m*/
    }
    return objectMapper;
}

From source file:com.strategicgains.restexpress.serialization.json.DefaultJsonProcessor.java

/**
 * Template method for sub-classes to augment the mapper with desired
 * settings.  Sub-classes should call super() to get default settings.
 * /*from   ww  w .ja v  a  2 s  .c  om*/
 * @param module a SimpleModule
 */
protected void initializeMapper(ObjectMapper mapper) {
    mapper
            //         .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
            .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)

            // Ignore additional/unknown properties in a payload.
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)

            // Only serialize populated properties (do no serialize nulls)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)

            // Use fields directly.
            .setVisibility(PropertyAccessor.FIELD, Visibility.ANY)

            // Ignore accessor and mutator methods (use fields per above).
            .setVisibility(PropertyAccessor.GETTER, Visibility.NONE)
            .setVisibility(PropertyAccessor.SETTER, Visibility.NONE)
            .setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE)

            // Set default date output format.
            .setDateFormat(new SimpleDateFormat(DateAdapterConstants.TIME_POINT_OUTPUT_FORMAT));
}

From source file:org.jasig.cas.util.AbstractJacksonBackedJsonSerializer.java

/**
 * Initialize object mapper./*from w  ww  .  ja  v a2  s  . c  o m*/
 *
 * @return the object mapper
 */
protected ObjectMapper initializeObjectMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC);
    mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    return mapper;
}

From source file:org.apereo.portal.dao.usertype.StatisticsJacksonColumnMapper.java

@Override
protected void customizeObjectMapper(ObjectMapper mapper) {
    //Just operate on fields
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);

    //Ignore the empty storedData field in all of the stat summary objects
    filters = new SimpleFilterProvider().addFilter(StoredDataFilterMixIn.FILTER_NAME,
            SimpleBeanPropertyFilter.serializeAllExcept("storedData"));
    mapper.addMixInAnnotations(Object.class, StoredDataFilterMixIn.class);
}

From source file:org.apereo.portal.events.aggr.JpaStatisticalSummaryTest.java

public void testStorelessUnivariateStatistic(StorelessUnivariateStatistic sus, double expected)
        throws Exception {

    assertEquals(expected, sus.getResult(), 0.1);

    final ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();/*from   w w w. jav  a  2 s  .  c  o m*/

    //Configure Jackson to just use fields
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);

    mapper.addMixInAnnotations(Object.class, IgnoreTypeMixIn.class);

    final FilterProvider filters = new SimpleFilterProvider().addFilter("storedDataFilter",
            SimpleBeanPropertyFilter.serializeAllExcept("storedData"));

    final ObjectWriter ssWriter = mapper.writer(filters);
    final ObjectReader ssReader = mapper.reader(sus.getClass());

    final String susString = ssWriter.writeValueAsString(sus);
    System.out.println(susString);
    final StorelessUnivariateStatistic newSus = ssReader.readValue(susString);

    assertEquals(expected, newSus.getResult(), 0.1);
}

From source file:org.finra.herd.tools.common.databridge.DataBridgeManifestReader.java

/**
 * Reads a JSON manifest file into a JSON manifest object.
 *
 * @param jsonManifestFile the JSON manifest file.
 *
 * @return the manifest object./*from  w ww.  j ava 2 s  .com*/
 * @throws java.io.IOException if any errors were encountered reading the JSON file.
 * @throws IllegalArgumentException if the manifest file has validation errors.
 */
public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException {
    // Verify that the file exists and can be read.
    HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile);

    // Deserialize the JSON manifest.
    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile));
    BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8));
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
    M manifest = getManifestFromReader(reader, objectMapper);

    // Validate the manifest and return it.
    validateManifest(manifest);
    return manifest;
}