public static FilterProvider getFilterProvider(final String... fieldsToExclude) { if (null == fieldsToExclude || fieldsToExclude.length == 0) { // Use the 'serializeAllExcept' method so it is compatible with older versions of jackson return new SimpleFilterProvider() .addFilter(FILTER_FIELDS_BY_NAME, (BeanPropertyFilter) SimpleBeanPropertyFilter.serializeAllExcept()); } return new SimpleFilterProvider() .addFilter(FILTER_FIELDS_BY_NAME, (BeanPropertyFilter) SimpleBeanPropertyFilter.serializeAllExcept(fieldsToExclude)); }
@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())); }
@Test public void filters() throws JsonProcessingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .filters(new SimpleFilterProvider().setFailOnUnknownId(false)).build(); JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2"); String output = objectMapper.writeValueAsString(bean); assertThat(output, containsString("value1")); assertThat(output, containsString("value2")); SimpleFilterProvider provider = new SimpleFilterProvider() .setFailOnUnknownId(false) .setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept("property2")); objectMapper = Jackson2ObjectMapperBuilder.json().filters(provider).build(); output = objectMapper.writeValueAsString(bean); assertThat(output, containsString("value1")); assertThat(output, not(containsString("value2"))); }
@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\""))); }
public static PropertyFilter createBuiltinFieldFilter() { return SimpleBeanPropertyFilter.serializeAllExcept(getBuiltinFieldNames()); }
public static PropertyFilter createBuiltinFieldFilter() { return SimpleBeanPropertyFilter.serializeAllExcept(getBuiltinFieldNames()); }
@Override public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String, Object> responseHeaders, Object valueToWrite, ObjectWriter w, JsonGenerator g) throws IOException { SimpleBeanPropertyFilter filter = null; if (includes != null && !includes.isEmpty()) { filter = new SimpleBeanPropertyFilter.FilterExceptFilter(includes); } else if (excludes != null && !excludes.isEmpty()) { filter = SimpleBeanPropertyFilter.serializeAllExcept(excludes); } else { filter = SimpleBeanPropertyFilter.serializeAllExcept(new HashSet<String>()); } FilterProvider provider = new SimpleFilterProvider().addFilter("property_filter", filter); return w.with(provider); } }
public static FilterProvider getFilterProvider(final String... fieldsToExclude) { if (null == fieldsToExclude || fieldsToExclude.length == 0) { // Use the 'serializeAllExcept' method so it is compatible with older versions of jackson return new SimpleFilterProvider() .addFilter(FILTER_FIELDS_BY_NAME, (BeanPropertyFilter) SimpleBeanPropertyFilter.serializeAllExcept()); } return new SimpleFilterProvider() .addFilter(FILTER_FIELDS_BY_NAME, (BeanPropertyFilter) SimpleBeanPropertyFilter.serializeAllExcept(fieldsToExclude)); }
@Override public void build(Writer writer) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); mapper.addMixInAnnotations(Object.class, DynamicPropertyFilterMixin.class); FilterProvider filterProvider = new SimpleFilterProvider() .addFilter("dynamicPropertyFilter", SimpleBeanPropertyFilter.serializeAllExcept(getExcludedProperties())); mapper.writer(filterProvider).writeValue(writer, getRootObject()); }
/** * Configure ObjectMapper to filter out fields causing serialization problems */ public SyndEntryJsonTransformer() { mapper = new ObjectMapper(); mapper.addMixInAnnotations(SyndEntry.class,this.getClass()); FilterProvider filterProvider = new SimpleFilterProvider() .addFilter("foreignMarkup filter", SimpleBeanPropertyFilter.serializeAllExcept("foreignMarkup")); mapper.setFilters(filterProvider); }
/** * Construct. */ public JsonRequestLogger() { mapper = new ObjectMapper(); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); SimpleFilterProvider filters = new SimpleFilterProvider(); filters.addFilter("default", SimpleBeanPropertyFilter.serializeAllExcept("eventTarget", "responseTarget")); mapper.setFilterProvider(filters); mapper.setAnnotationIntrospector(new FilteredIntrospector()); }
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()); } 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); }
protected ObjectWriter configureObjectMapper(final ObjectMapper jsonObjectMapper) { jsonObjectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); jsonObjectMapper.setDateFormat(new StdDateFormat()); 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", "validInIosVersionsBefore9")); 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); }
/** * 过滤属性 * <pre> * 使用方法:先给ObjectMapper添加一个filter,然后还要在需要过滤的类上加@JsonFilter("filterName")注解。 * 比如说要过滤User 上的name属性,先 * Jacksons.json().filter("myFilter", "name").readAsString(user),具体看Jacksons代码。并在User类上加@JsonFilter("myFilter")。 * 有点不爽的是如果用另外一个没有添加该filter的ObjectMapper解析的话会报错。 * 如果这个User类已经添加了@JsonFilter("myFilter")注解,但在另外一个地方又要解析它并不想过滤name 属性,那只能是 * Jacksons.json().filter("myFilter", ""),然后在读出来。 * </pre> * * @param filterName 过滤器的名称 * @param properties 要过滤的字段名称 * @return */ public Jacksons filter(String filterName, String... properties) { FilterProvider filterProvider = new SimpleFilterProvider().addFilter(filterName, SimpleBeanPropertyFilter.serializeAllExcept(properties)); objectMapper.setFilterProvider(filterProvider); return this; }
private String toJson(Object instance) throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.addMixIn(Object.class, PropertyFilterMixIn.class); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter .serializeAllExcept("lastUpdated"); FilterProvider filters = new SimpleFilterProvider() .addFilter("skipLastUpdated", theFilter); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); return objectMapper.writer(filters).writeValueAsString(instance); } }
/** * 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 */ 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.setFilterProvider(simpleFilterProvider); _redactEnabled = redactEnabled; }
protected void init() { mapper.addMixInAnnotations(Object.class, PropertyFilterMixIn.class); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE); mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.SETTER, Visibility.PROTECTED_AND_PUBLIC); registerModules(mapper); mapper.configure(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS, true); mapper.setPropertyNamingStrategy(getPropertyNamingStrategy()); SimpleFilterProvider provider = new SimpleFilterProvider(); provider.addFilter("property_filter", SimpleBeanPropertyFilter.serializeAllExcept(Sets.<String>newHashSet())); mapper.setFilters(provider); }
@Override public void setupModule(SetupContext context) { context.setMixInAnnotations(Object.class, PropertyFilterMixIn.class); ((ObjectMapper) context.getOwner()).setFilters(new SimpleFilterProvider().addFilter("property_filter", SimpleBeanPropertyFilter.serializeAllExcept(new String[0]))); super.setupModule(context); } }
@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); }
@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); }