Search in sources :

Example 6 with FilterProvider

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

the class AbstractJackson2View method filterAndWrapModel.

/**
	 * Filter and optionally wrap the model in {@link MappingJacksonValue} container.
	 * @param model the model, as passed on to {@link #renderMergedOutputModel}
	 * @param request current HTTP request
	 * @return the wrapped or unwrapped value to be rendered
	 */
protected Object filterAndWrapModel(Map<String, Object> model, HttpServletRequest request) {
    Object value = filterModel(model);
    Class<?> serializationView = (Class<?>) model.get(JsonView.class.getName());
    FilterProvider filters = (FilterProvider) model.get(FilterProvider.class.getName());
    if (serializationView != null || filters != null) {
        MappingJacksonValue container = new MappingJacksonValue(value);
        container.setSerializationView(serializationView);
        container.setFilters(filters);
        value = container;
    }
    return value;
}
Also used : MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 7 with FilterProvider

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

the class MappingJackson2JsonViewTests method renderSimpleBeanWithFilters.

@Test
public void renderSimpleBeanWithFilters() throws Exception {
    TestSimpleBeanFiltered bean = new TestSimpleBeanFiltered();
    bean.setProperty1("value");
    bean.setProperty2("value");
    Map<String, Object> model = new HashMap<>();
    model.put("bindingResult", mock(BindingResult.class, "binding_result"));
    model.put("foo", bean);
    FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2"));
    model.put(FilterProvider.class.getName(), filters);
    view.setUpdateContentLength(true);
    view.render(model, request, response);
    String content = response.getContentAsString();
    assertTrue(content.length() > 0);
    assertEquals(content.length(), response.getContentLength());
    assertThat(content, containsString("\"property1\":\"value\""));
    assertThat(content, not(containsString("\"property2\":\"value\"")));
    assertFalse(content.contains(FilterProvider.class.getName()));
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) BindingResult(org.springframework.validation.BindingResult) HashMap(java.util.HashMap) ScriptableObject(org.mozilla.javascript.ScriptableObject) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.Test)

Example 8 with FilterProvider

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

the class TestAnyGetterFiltering method testAnyGetterFiltering.

/*
    /**********************************************************
    /* Test methods
    /**********************************************************
     */
public void testAnyGetterFiltering() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    FilterProvider prov = new SimpleFilterProvider().addFilter("anyFilter", SimpleBeanPropertyFilter.filterOutAllExcept("b"));
    assertEquals("{\"b\":\"2\"}", mapper.writer(prov).writeValueAsString(new AnyBean()));
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Example 9 with FilterProvider

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

the class TestJsonFilter method testIncludeAllFilter.

public void testIncludeAllFilter() throws Exception {
    FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter", SimpleBeanPropertyFilter.serializeAll());
    assertEquals("{\"a\":\"a\",\"b\":\"b\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}
Also used : FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 10 with FilterProvider

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

the class TestJsonFilter method testFilterOnProperty.

// Wrt [Issue#306]
public void testFilterOnProperty() throws Exception {
    FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter", SimpleBeanPropertyFilter.filterOutAllExcept("a")).addFilter("b", SimpleBeanPropertyFilter.filterOutAllExcept("b"));
    assertEquals("{\"first\":{\"a\":\"a\"},\"second\":{\"b\":\"b\"}}", MAPPER.writer(prov).writeValueAsString(new FilteredProps()));
}
Also used : 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