|
| 1 | +package com.fasterxml.jackson.dataformat.xml.ser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonFilter; |
| 4 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 5 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 6 | +import com.fasterxml.jackson.databind.ser.FilterProvider; |
| 7 | +import com.fasterxml.jackson.databind.ser.PropertyFilter; |
| 8 | +import com.fasterxml.jackson.databind.ser.PropertyWriter; |
| 9 | +import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; |
| 10 | +import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; |
| 11 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 12 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 13 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; |
| 14 | + |
| 15 | +/** |
| 16 | + * Unit test for [PullRequest#616], problems with filtered serialization. |
| 17 | + */ |
| 18 | +public class TestSerializationWithFilter extends XmlTestBase |
| 19 | +{ |
| 20 | + @JsonFilter("filter") |
| 21 | + static class Item |
| 22 | + { |
| 23 | + @JacksonXmlText |
| 24 | + public int a; |
| 25 | + public int b; |
| 26 | + public int c; |
| 27 | + } |
| 28 | + |
| 29 | + public void testPullRequest616() throws Exception |
| 30 | + { |
| 31 | + Item bean = new Item(); |
| 32 | + bean.a = 0; |
| 33 | + bean.b = 10; |
| 34 | + bean.c = 100; |
| 35 | + |
| 36 | + String exp = "<Item><b>10</b><c>100</c></Item>"; |
| 37 | + |
| 38 | + XmlMapper xmlMapper = new XmlMapper(); |
| 39 | + PropertyFilter filter = new SimpleBeanPropertyFilter() { |
| 40 | + @Override |
| 41 | + public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception |
| 42 | + { |
| 43 | + if (include(writer) && writer.getName().equals("a")) { |
| 44 | + int a = ((Item) pojo).a; |
| 45 | + if (a <= 0) |
| 46 | + return; |
| 47 | + } |
| 48 | + super.serializeAsField(pojo, jgen, provider, writer); |
| 49 | + } |
| 50 | + }; |
| 51 | + FilterProvider filterProvider = new SimpleFilterProvider().addFilter("filter", filter); |
| 52 | + xmlMapper.setFilterProvider(filterProvider); |
| 53 | + String act = xmlMapper.writeValueAsString(bean); |
| 54 | + assertEquals(exp, act); |
| 55 | + } |
| 56 | +} |
0 commit comments