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

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

Introduction

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

Prototype

public ObjectMapper setVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility) 

Source Link

Document

Convenience method that allows changing configuration for underlying VisibilityChecker s, to change details of what kinds of properties are auto-detected.

Usage

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

/**
 * Initialize object mapper./* w  ww .  java2s  .  co 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:io.gumga.security.WebConfigForTest.java

private MappingJackson2HttpMessageConverter jacksonConverter() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new Hibernate4Module());
    mapper.registerModule(new JodaModule());
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.setDateFormat(new ISO8601DateFormat());
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

    MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    jacksonConverter.setObjectMapper(mapper);

    return jacksonConverter;
}

From source file:de.ks.flatadocdb.index.GlobalIndex.java

protected ObjectMapper getMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();//from w w w  .  ja v  a 2 s  . c  o  m
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping();
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
    return mapper;
}

From source file:jp.classmethod.aws.brian.utils.BrianServerObjectMapperFactoryBean.java

@Override
protected ObjectMapper createInstance() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule brianModule = new SimpleModule("brianServerModule", VERSION);
    brianModule.addSerializer(Trigger.class, new TriggerSerializer());
    mapper.registerModule(brianModule);/*  w  w  w  . j  a  va  2s .  c om*/
    mapper.registerModule(new Jdk8Module());

    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    df.setTimeZone(TimeZones.UNIVERSAL);
    mapper.setDateFormat(df);

    return mapper;
}

From source file:org.commonjava.cartographer.client.CartographerRESTClient.java

public ObjectMapper newObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModules(new CartoAPIObjectMapperModules().getSerializerModules());
    mapper.setVisibility(ALL, NONE);
    mapper.setVisibility(FIELD, ANY);//from  w ww . j  a va 2 s  .  c o m
    mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
    return mapper;
}

From source file:com.epam.marshaller.jackson.JacksonMarshallerTest.java

private ObjectMapper prepareObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
    objectMapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS);
    objectMapper.enable(MapperFeature.AUTO_DETECT_FIELDS);
    objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
    objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

    return objectMapper;
}

From source file:com.ikanow.aleph2.data_model.utils.TestBeanTemplateUtils.java

@Test
public void testNumberDeserializer() {
    TestDeserializeBean bean1 = BeanTemplateUtils
            .from("{\"testMap\":{\"test\":1.0}}", TestDeserializeBean.class).get();
    JsonNode jn1 = BeanTemplateUtils.toJson(bean1);

    assertEquals("{\"testMap\":{\"test\":1}}", jn1.toString());

    TestDeserializeBean bean2 = BeanTemplateUtils.from(
            "{\"testMap\":{\"test\":10000000000000,\"test1\":10000000000000.1,\"test2\":\"some string\"}}",
            TestDeserializeBean.class).get();
    JsonNode jn2 = BeanTemplateUtils.toJson(bean2);

    assertEquals(/*from w  ww.  j  a va  2 s  .  c o  m*/
            "{\"testMap\":{\"test\":10000000000000,\"test1\":1.00000000000001E13,\"test2\":\"some string\"}}",
            jn2.toString());

    TestDeserializeBean bean3 = BeanTemplateUtils
            .from("{\"testMap\":{\"test\":1e7, \"test2\":1.0000000000000E7,\"test3\":1E19}}",
                    TestDeserializeBean.class)
            .get();
    JsonNode jn3 = BeanTemplateUtils.toJson(bean3);

    assertEquals("{\"testMap\":{\"test\":10000000,\"test2\":1.0E7,\"test3\":1.0E19}}", jn3.toString());

    //Mapper without the Number deserializer to make sure the double values are staying the same
    ObjectMapper plainMapper = new ObjectMapper();
    plainMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    plainMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    plainMapper.setSerializationInclusion(Include.NON_NULL);

    //Checking that the deserializer doesn't break existing double values
    TestDeserializeBean bean4 = BeanTemplateUtils.from(
            "{\"testMap\":{\"test\":1e7, \"test2\":1.0000000000000E7,\"test3\":1.1,\"test4\":10000000000000.1,\"test4\":6e9,\"test5\":6e300,\"test5\":1E7 }}",
            TestDeserializeBean.class).get();
    JsonNode number_deserialized = BeanTemplateUtils.toJson(bean4);
    JsonNode plain = plainMapper.valueToTree(bean4);

    assertEquals(plain.toString(), number_deserialized.toString());

    // Just check it doesn't mess up actual double deserialization:

    TestDeserializeBean2 bean2_1 = BeanTemplateUtils
            .from("{\"testMap\":{\"test\":1.0}}", TestDeserializeBean2.class).get();
    JsonNode jn2_1 = BeanTemplateUtils.toJson(bean2_1);

    assertEquals("{\"testMap\":{\"test\":1.0}}", jn2_1.toString());

}

From source file:com.josue.tileset.editor.Editor.java

private void exportBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportBtnActionPerformed

    JFileChooser fileDialog = new JFileChooser();
    fileDialog.setFileFilter(new FileNameExtensionFilter("Json file", "json"));
    String outputFileName = inputFile.getAbsolutePath().split("\\.")[0] + ".json";
    fileDialog.setSelectedFile(new File(outputFileName));
    if (fileDialog.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {

        File file = fileDialog.getSelectedFile();
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
        try {// w  ww  .  j av  a 2s. c o m
            mapper.writeValue(file, loadedTiles);
        } catch (IOException ex) {
            Logger.getLogger(Editor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:org.apache.storm.hbase.state.HBaseKeyValueStateProvider.java

StateConfig getStateConfig(Map stormConf) throws Exception {
    StateConfig stateConfig;//from ww w .j  a  v  a2  s . c o m
    String providerConfig;
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    if (stormConf.containsKey(Config.TOPOLOGY_STATE_PROVIDER_CONFIG)) {
        providerConfig = (String) stormConf.get(Config.TOPOLOGY_STATE_PROVIDER_CONFIG);
        stateConfig = mapper.readValue(providerConfig, StateConfig.class);
    } else {
        stateConfig = new StateConfig();
    }

    // assertion
    assertMandatoryParameterNotEmpty(stateConfig.hbaseConfigKey, "hbaseConfigKey");
    assertMandatoryParameterNotEmpty(stateConfig.tableName, "tableName");
    assertMandatoryParameterNotEmpty(stateConfig.columnFamily, "columnFamily");

    return stateConfig;
}

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);
}