Search in sources :

Example 1 with FilterProvider

use of com.fasterxml.jackson.databind.ser.FilterProvider in project photo-picker-plus-android by chute.

the class MediaModel method serializeImageDataModel.

public String serializeImageDataModel() {
    FilterProvider filters = new SimpleFilterProvider().addFilter("imageDataModelFilter", SimpleBeanPropertyFilter.filterOutAllExcept("options", "media"));
    String result = null;
    try {
        result = JsonUtil.getMapper().writer(filters).writeValueAsString(this);
    } catch (JsonProcessingException e) {
        Log.d(TAG, "", e);
    }
    return result;
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Example 2 with FilterProvider

use of com.fasterxml.jackson.databind.ser.FilterProvider in project jersey by jersey.

the class FilteringJacksonJaxbJsonProvider method writeTo.

@Override
public void writeTo(final Object value, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException {
    final FilterProvider filterProvider = provider.get().getFilteringObject(genericType, true, annotations);
    if (filterProvider != null) {
        ObjectWriterInjector.set(new FilteringObjectWriterModifier(filterProvider, ObjectWriterInjector.getAndClear()));
    }
    super.writeTo(value, type, genericType, annotations, mediaType, httpHeaders, entityStream);
}
Also used : FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 3 with FilterProvider

use of com.fasterxml.jackson.databind.ser.FilterProvider in project spring-framework by spring-projects.

the class AbstractJackson2HttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        writePrefix(generator, object);
        Class<?> serializationView = null;
        FilterProvider filters = null;
        Object value = object;
        JavaType javaType = null;
        if (object instanceof MappingJacksonValue) {
            MappingJacksonValue container = (MappingJacksonValue) object;
            value = container.getValue();
            serializationView = container.getSerializationView();
            filters = container.getFilters();
        }
        if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
            javaType = getJavaType(type, null);
        }
        ObjectWriter objectWriter;
        if (serializationView != null) {
            objectWriter = this.objectMapper.writerWithView(serializationView);
        } else if (filters != null) {
            objectWriter = this.objectMapper.writer(filters);
        } else {
            objectWriter = this.objectMapper.writer();
        }
        if (javaType != null && javaType.isContainerType()) {
            objectWriter = objectWriter.forType(javaType);
        }
        SerializationConfig config = objectWriter.getConfig();
        if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            objectWriter = objectWriter.with(this.ssePrettyPrinter);
        }
        objectWriter.writeValue(generator, value);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 4 with FilterProvider

use of com.fasterxml.jackson.databind.ser.FilterProvider in project spring-framework by spring-projects.

the class MappingJackson2HttpMessageConverterTests method filters.

@Test
public void filters() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    JacksonFilteredBean bean = new JacksonFilteredBean();
    bean.setProperty1("value");
    bean.setProperty2("value");
    MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
    FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2"));
    jacksonValue.setFilters(filters);
    this.converter.writeInternal(jacksonValue, null, outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertThat(result, containsString("\"property1\":\"value\""));
    assertThat(result, not(containsString("\"property2\":\"value\"")));
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.Test)

Example 5 with FilterProvider

use of com.fasterxml.jackson.databind.ser.FilterProvider in project spring-framework by spring-projects.

the class AbstractJackson2View method writeContent.

/**
	 * Write the actual JSON content to the stream.
	 * @param stream the output stream to use
	 * @param object the value to be rendered, as returned from {@link #filterModel}
	 * @throws IOException if writing failed
	 */
protected void writeContent(OutputStream stream, Object object) throws IOException {
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(stream, this.encoding);
    writePrefix(generator, object);
    Class<?> serializationView = null;
    FilterProvider filters = null;
    Object value = object;
    if (value instanceof MappingJacksonValue) {
        MappingJacksonValue container = (MappingJacksonValue) value;
        value = container.getValue();
        serializationView = container.getSerializationView();
        filters = container.getFilters();
    }
    if (serializationView != null) {
        this.objectMapper.writerWithView(serializationView).writeValue(generator, value);
    } else if (filters != null) {
        this.objectMapper.writer(filters).writeValue(generator, value);
    } else {
        this.objectMapper.writeValue(generator, value);
    }
    writeSuffix(generator, object);
    generator.flush();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Aggregations

FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)22 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)4 Test (org.junit.Test)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 MappingJacksonValue (org.springframework.http.converter.json.MappingJacksonValue)2 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 HashMap (java.util.HashMap)1 StorelessUnivariateStatistic (org.apache.commons.math3.stat.descriptive.StorelessUnivariateStatistic)1 Ignore (org.junit.Ignore)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1 MediaType (org.springframework.http.MediaType)1 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1