Example usage for com.fasterxml.jackson.databind.ser.impl SimpleFilterProvider addFilter

List of usage examples for com.fasterxml.jackson.databind.ser.impl SimpleFilterProvider addFilter

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.ser.impl SimpleFilterProvider addFilter.

Prototype

public SimpleFilterProvider addFilter(String id, SimpleBeanPropertyFilter filter) 

Source Link

Document

Overloaded variant just to resolve "ties" when using SimpleBeanPropertyFilter .

Usage

From source file:com.kingen.web.CommonController.java

public static String serializeOnlyGivenFields(Object o, Collection<String> fields)
        throws JsonProcessingException {
    if ((fields == null) || fields.isEmpty())
        fields = new HashSet<String>();

    Set<String> properties = new HashSet<String>(fields);

    SimpleBeanPropertyFilter filter = new SimpleBeanPropertyFilter.FilterExceptFilter(properties);
    SimpleFilterProvider fProvider = new SimpleFilterProvider();
    fProvider.addFilter(FILTER_NAME, filter);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setAnnotationIntrospector(new AnnotationIntrospector());

    String json = mapper.writer(fProvider).writeValueAsString(o);
    return json;//from w  ww.  j  a  v a 2 s. c  o m
}

From source file:org.camunda.bpm.elasticsearch.jackson.JacksonMixInFilterModule.java

public static SimpleFilterProvider getCustomFilterProvider() {
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    for (Map.Entry<Class<? extends HistoryEvent>, Class> mixInFilter : getDefaultMixInFilters().entrySet()) {
        String jsonFilterId = getJsonFilterAnnotationValue(mixInFilter.getValue());
        if (jsonFilterId != null) {
            SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAllExcept("");
            // TODO: add extension mechanism to declare filter values via ElasticSearchHistoryPluginConfiguration
            filterProvider.addFilter(jsonFilterId, filter);
        }//from   w  w  w  .j a v a 2  s .  co  m
    }

    return filterProvider;
}

From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java

private static void createPassJSONFile(final PKPass pass, final File tempPassDir,
        final ObjectMapper jsonObjectMapper) throws IOException, JsonGenerationException, JsonMappingException {
    File passJSONFile = new File(tempPassDir.getAbsolutePath() + File.separator + PASS_JSON_FILE_NAME);

    SimpleFilterProvider filters = new SimpleFilterProvider();

    // haven't found out, how to stack filters. Copying the validation one for now.
    filters.addFilter("validateFilter",
            SimpleBeanPropertyFilter.serializeAllExcept("valid", "validationErrors"));
    filters.addFilter("pkPassFilter", SimpleBeanPropertyFilter.serializeAllExcept("valid", "validationErrors",
            "foregroundColorAsObject", "backgroundColorAsObject", "labelColorAsObject"));
    filters.addFilter("barcodeFilter", SimpleBeanPropertyFilter.serializeAllExcept("valid", "validationErrors",
            "messageEncodingAsString"));
    filters.addFilter("charsetFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
    jsonObjectMapper.setSerializationInclusion(Include.NON_NULL);
    jsonObjectMapper.addMixInAnnotations(Object.class, ValidateFilterMixIn.class);
    jsonObjectMapper.addMixInAnnotations(PKPass.class, PkPassFilterMixIn.class);
    jsonObjectMapper.addMixInAnnotations(PKBarcode.class, BarcodeFilterMixIn.class);
    jsonObjectMapper.addMixInAnnotations(Charset.class, CharsetFilterMixIn.class);

    ObjectWriter objectWriter = jsonObjectMapper.writer(filters);
    objectWriter.writeValue(passJSONFile, pass);
}

From source file:org.craftercms.commons.jackson.mvc.SelectorFilterFactory.java

@Override
protected FilterProvider createInstance() throws Exception {
    SimpleFilterProvider provider = new SimpleFilterProvider();
    for (AbstractCrafterPropertyFilter filter : filters) {
        provider.addFilter(filter.getFilterName(), filter);
    }/*from  w ww.  ja  v a  2  s .c o m*/
    if (!filters.isEmpty()) {
        provider.setDefaultFilter(filters.get(0));
    }
    return provider;
}

From source file:com.arpnetworking.logback.jackson.LogValueTest.java

@Before
public void setUp() {
    _objectMapper = new ObjectMapper();
    _objectMapper.setAnnotationIntrospector(new StenoAnnotationIntrospector());
    final SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider();
    simpleFilterProvider.addFilter(RedactionFilter.REDACTION_FILTER_ID, new RedactionFilter(false));
    _objectMapper.setFilters(simpleFilterProvider);
}

From source file:de.brendamour.jpasskit.signing.PKAbstractSIgningUtil.java

protected ObjectWriter configureObjectMapper(final ObjectMapper jsonObjectMapper) {
    jsonObjectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    jsonObjectMapper.setDateFormat(new ISO8601DateFormat());

    SimpleFilterProvider filters = new SimpleFilterProvider();

    // haven't found out, how to stack filters. Copying the validation one for now.
    filters.addFilter("validateFilter",
            SimpleBeanPropertyFilter.serializeAllExcept("valid", "validationErrors"));
    filters.addFilter("pkPassFilter", SimpleBeanPropertyFilter.serializeAllExcept("valid", "validationErrors",
            "foregroundColorAsObject", "backgroundColorAsObject", "labelColorAsObject", "passThatWasSet"));
    filters.addFilter("barcodeFilter", SimpleBeanPropertyFilter.serializeAllExcept("valid", "validationErrors",
            "messageEncodingAsString"));
    filters.addFilter("charsetFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
    jsonObjectMapper.setSerializationInclusion(Include.NON_NULL);
    jsonObjectMapper.addMixIn(Object.class, ValidateFilterMixIn.class);
    jsonObjectMapper.addMixIn(PKPass.class, PkPassFilterMixIn.class);
    jsonObjectMapper.addMixIn(PKBarcode.class, BarcodeFilterMixIn.class);
    jsonObjectMapper.addMixIn(Charset.class, CharsetFilterMixIn.class);
    return jsonObjectMapper.writer(filters);
}

From source file:com.ryo.log4j2.context.core.JacksonFactory.java

ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact) {
    final SimpleFilterProvider filters = new SimpleFilterProvider();
    final Set<String> except = new HashSet<>(2);
    if (!locationInfo) {
        except.add(this.getPropertNameForSource());
    }/*  ww  w. j av a 2 s  .c o m*/
    if (!properties) {
        except.add(this.getPropertNameForContextMap());
    }
    except.add(this.getPropertNameForNanoTime());
    filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
    final ObjectWriter writer = this.newObjectMapper()
            .writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
    return writer.with(filters);
}

From source file:com.arpnetworking.logback.StenoEncoder.java

/**
 * Enables/Disables output of null for redacted fields when serializing complex objects.
 *
 * @param redactNull - true to redact null values, assuming redactEnabled is true
 *
 * @since 1.1.0/*from  w w w.j a  va2 s .c o  m*/
 */
public void setRedactNull(final boolean redactNull) {
    if (_redactEnabled) {
        final SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider();
        simpleFilterProvider.addFilter(RedactionFilter.REDACTION_FILTER_ID, new RedactionFilter(!redactNull));
        _objectMapper.setFilters(simpleFilterProvider);
    }
    _redactNull = redactNull;
}

From source file:com.arpnetworking.logback.StenoEncoder.java

StenoEncoder(final JsonFactory jsonFactory, final ObjectMapper objectMapper) {

    // Initialize object mapper;
    _objectMapper = objectMapper;//from www. j  a  va2s .  com
    _objectMapper.setAnnotationIntrospector(new StenoAnnotationIntrospector());
    final SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider();
    simpleFilterProvider.addFilter(RedactionFilter.REDACTION_FILTER_ID,
            new RedactionFilter(!DEFAULT_REDACT_NULL));
    // Initialize this here based on the above code, if it was initialized at the declaration site then things
    // could get out of sync
    _redactEnabled = true;
    _objectMapper.setFilters(simpleFilterProvider);

    // Setup writing of Date/DateTime values
    _objectMapper.registerModule(new JodaModule());
    _objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    _objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    _objectMapper.setDateFormat(new ISO8601DateFormat());

    // Setup other common modules
    _objectMapper.registerModule(new AfterburnerModule());
    _objectMapper.registerModule(new Jdk7Module());
    _objectMapper.registerModule(new Jdk8Module());
    _objectMapper.registerModule(new GuavaModule());

    // Serialization strategies
    _listsSerialziationStrategy = new ListsSerialziationStrategy(this, jsonFactory, _objectMapper);
    _objectAsJsonSerialziationStrategy = new ObjectAsJsonSerialziationStrategy(this, jsonFactory,
            _objectMapper);
    _objectSerialziationStrategy = new ObjectSerialziationStrategy(this, jsonFactory, _objectMapper);
    _mapOfJsonSerialziationStrategy = new MapOfJsonSerialziationStrategy(this, jsonFactory, _objectMapper);
    _mapSerialziationStrategy = new MapSerialziationStrategy(this, jsonFactory, _objectMapper);
    _arrayOfJsonSerialziationStrategy = new ArrayOfJsonSerialziationStrategy(this, jsonFactory, _objectMapper);
    _arraySerialziationStrategy = new ArraySerialziationStrategy(this, jsonFactory, _objectMapper);
    _standardSerializationStrategy = new StandardSerializationStrategy(this, jsonFactory, _objectMapper);
}

From source file:com.arpnetworking.logback.StenoEncoder.java

/**
 * Enables/Disables redaction support when serializing complex objects.  Redacted fields/properties marked
 * with the @LogRedact annotation will be output as a string with the value "{@code<REDACTED>}".
 *
 * @param redactEnabled - true to filter out redacted fields
 *
 * @since 1.1.0/* w w w.j  a  va2  s  .co m*/
 */
public void setRedactEnabled(final boolean redactEnabled) {
    final SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider();
    if (redactEnabled) {
        simpleFilterProvider.addFilter(RedactionFilter.REDACTION_FILTER_ID, new RedactionFilter(!_redactNull));
    } else {
        simpleFilterProvider.addFilter(RedactionFilter.REDACTION_FILTER_ID,
                SimpleBeanPropertyFilter.serializeAllExcept(Collections.<String>emptySet()));
    }
    _objectMapper.setFilters(simpleFilterProvider);
    _redactEnabled = redactEnabled;
}