Hello, I have the case where I want to use a JsonFilter to exclude a property from being serialized to xml. Everything is ok when all the properties of the bean are to be serialized as xml child elements.
However, when there exists a bean property that needs to be serialised as attribute, then the "isAttribute" annotation gets ignored.
Please find a test case below.
Thank you in advance for your help.
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
public class JsonFilterWithXmlAttributeTest {
@JsonFilter("test-filter")
@JsonPropertyOrder({ "id", "name"})
abstract class TestFilterMixin{}
@JsonPropertyOrder({ "id", "name", "jsonProp" })
abstract class TestNoFilterMixin{}
@JsonIgnoreProperties(ignoreUnknown=true)
static class TestCls{
private String id;
private String name;
private String jsonProp;
public TestCls(){}
public TestCls( String id, String name, String jsonProp ){
this.id= id;
this.name = name;
this.jsonProp = jsonProp;
}
@JacksonXmlProperty(isAttribute=true)
public String getId() {return id;}
public void setId(String id) {this.id = id;}
@JsonProperty("name")
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@JsonProperty("jsonProp")
public String getJsonProp() {return jsonProp;}
public void setJsonProp(String jsonProp) {this.jsonProp = jsonProp;}
}
@Test
public void testFilter() throws JsonGenerationException, JsonMappingException, IOException{
XmlMapper mapper = new XmlMapper();
mapper.addMixInAnnotations( TestCls.class, TestFilterMixin.class);
FilterProvider filters = new SimpleFilterProvider().addFilter("test-filter",
SimpleBeanPropertyFilter.serializeAllExcept("jsonProp"));
mapper.setFilters(filters);
TestCls test = new TestCls( "123", "tester", "jsonText");
String xml = mapper.writeValueAsString(test);
assertThat("xml", xml, is("<TestCls id=\"123\"><name>tester</name></TestCls>") );
}
@Test
public void testNoFilter() throws JsonGenerationException, JsonMappingException, IOException{
XmlMapper mapper = new XmlMapper();
mapper.addMixInAnnotations( TestCls.class, TestNoFilterMixin.class);
TestCls test = new TestCls( "123", "tester", "jsonText");
String xml = mapper.writeValueAsString(test);
assertThat("xml", xml, is("<TestCls id=\"123\"><name>tester</name><jsonProp>jsonText</jsonProp></TestCls>") );
}
}