jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

JsonFilter with XmlMapper ignores xml attributes

Open farfalena-zz opened this issue 14 years ago • 1 comments

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>") );

    }
}

farfalena-zz avatar Sep 20 '12 11:09 farfalena-zz

I think this is same as https://github.com/FasterXML/jackson-dataformat-xml/issues/35 which I just fixed!

cowtowncoder avatar Nov 22 '12 02:11 cowtowncoder