Example usage for com.fasterxml.jackson.databind.ser.impl SimpleBeanPropertyFilter filterOutAllExcept

List of usage examples for com.fasterxml.jackson.databind.ser.impl SimpleBeanPropertyFilter filterOutAllExcept

Introduction

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

Prototype

public static SimpleBeanPropertyFilter filterOutAllExcept(String... propertyArray) 

Source Link

Usage

From source file:org.oncoblocks.centromere.web.util.FilteringJackson2HttpMessageConverter.java

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    ObjectMapper objectMapper = getObjectMapper();
    JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(outputMessage.getBody());

    try {// w  ww  .  j av a 2  s. co m

        if (this.prefixJson) {
            jsonGenerator.writeRaw(")]}', ");
        }

        if (object instanceof ResponseEnvelope) {

            ResponseEnvelope envelope = (ResponseEnvelope) object;
            Object entity = envelope.getEntity();
            Set<String> fieldSet = envelope.getFieldSet();
            Set<String> exclude = envelope.getExclude();
            FilterProvider filters = null;

            if (fieldSet != null && !fieldSet.isEmpty()) {
                if (entity instanceof ResourceSupport) {
                    fieldSet.add("content"); // Don't filter out the wrapped content.
                }
                filters = new SimpleFilterProvider()
                        .addFilter("fieldFilter", SimpleBeanPropertyFilter.filterOutAllExcept(fieldSet))
                        .setFailOnUnknownId(false);
            } else if (exclude != null && !exclude.isEmpty()) {
                filters = new SimpleFilterProvider()
                        .addFilter("fieldFilter", SimpleBeanPropertyFilter.serializeAllExcept(exclude))
                        .setFailOnUnknownId(false);
            } else {
                filters = new SimpleFilterProvider()
                        .addFilter("fieldFilter", SimpleBeanPropertyFilter.serializeAllExcept())
                        .setFailOnUnknownId(false);
            }

            objectMapper.setFilterProvider(filters);
            objectMapper.writeValue(jsonGenerator, entity);

        } else if (object == null) {
            jsonGenerator.writeNull();
        } else {
            FilterProvider filters = new SimpleFilterProvider().setFailOnUnknownId(false);
            objectMapper.setFilterProvider(filters);
            objectMapper.writeValue(jsonGenerator, object);
        }

    } catch (JsonProcessingException e) {
        e.printStackTrace();
        throw new HttpMessageNotWritableException("Could not write JSON: " + e.getMessage());
    }

}

From source file:com.github.darwinevolution.darwin.jackson.JacksonEvolutionResultConsumer.java

@Override
public void consumeResults(ComparisonResult<T> comparisonResult,
        ResultConsumerConfiguration resultConsumerConfiguration) {
    try {//from  www .  j  a va2 s .  com
        SimpleFilterProvider fp = new SimpleFilterProvider();
        Set<String> includedFields = new HashSet<String>();
        includedFields.add("name");
        includedFields.add("implementationPreference");
        includedFields.add("timestamp");
        includedFields.add("resultType");
        includedFields.add("protoplastDurationNs");
        includedFields.add("evolvedDurationNs");

        if (comparisonResult.getResultType().isError()
                && resultConsumerConfiguration.shouldPrintDetailsOnError()) {
            includedFields.add("protoplastArguments");
            includedFields.add("protoplastValue");
            includedFields.add("protplastException");

            includedFields.add("evolvedArguments");
            includedFields.add("evolvedValue");
            includedFields.add("evolvedException");
        }

        if (resultConsumerConfiguration.shouldPrintBothArguments()) {
            includedFields.add("protoplastArguments");
            includedFields.add("evolvedArguments");
        }

        if (resultConsumerConfiguration.shouldPrintResults()) {
            includedFields.add("protoplastValue");
            includedFields.add("protplastException");
            includedFields.add("evolvedValue");
            includedFields.add("evolvedException");
        }

        fp.addFilter(FILTER_NAME, SimpleBeanPropertyFilter.filterOutAllExcept(includedFields));
        String json = objectMapper.writer(fp)
                .writeValueAsString(JsonComparisonResult.from(comparisonResult, resultConsumerConfiguration));
        evolutionLog.debug(json);
    } catch (JsonProcessingException e) {
        log.error("Error creating json comparison result", e);
    }
}

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:org.osiam.resource_server.resources.helper.AttributesRemovalHelper.java

private ObjectWriter getObjectWriter(ObjectMapper mapper, String[] fieldsToReturn) {

    if (fieldsToReturn.length != 0) {
        mapper.addMixInAnnotations(Object.class, PropertyFilterMixIn.class);

        HashSet<String> givenFields = new HashSet<String>();
        givenFields.add("schemas");
        for (String field : fieldsToReturn) {
            givenFields.add(field);/*w w  w .  j  a v a  2s  .co  m*/
        }
        String[] finalFieldsToReturn = givenFields.toArray(new String[givenFields.size()]);

        FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name",
                SimpleBeanPropertyFilter.filterOutAllExcept(finalFieldsToReturn));
        return mapper.writer(filters);
    }
    return mapper.writer();
}

From source file:org.osiam.resources.helper.AttributesRemovalHelper.java

private ObjectWriter getObjectWriter(ObjectMapper mapper, String[] fieldsToReturn) {

    if (fieldsToReturn.length != 0) {
        mapper.addMixInAnnotations(Object.class, PropertyFilterMixIn.class);

        HashSet<String> givenFields = new HashSet<>();
        givenFields.add("schemas");
        Collections.addAll(givenFields, fieldsToReturn);
        String[] finalFieldsToReturn = givenFields.toArray(new String[givenFields.size()]);

        FilterProvider filters = new SimpleFilterProvider().addFilter("filter properties by name",
                SimpleBeanPropertyFilter.filterOutAllExcept(finalFieldsToReturn));
        return mapper.writer(filters);
    }/*from   w  w  w . ja  v a2  s.  co m*/
    return mapper.writer();
}

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