-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mismatch in setNextIsUnwrapped(true/false) in XmlBeanSerializerBa…
…se#serializeFieldsFiltered() (#616)
- Loading branch information
1 parent
3d2ec75
commit b7e70d0
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestSerializationWithFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.fasterxml.jackson.dataformat.xml.ser; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFilter; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.FilterProvider; | ||
import com.fasterxml.jackson.databind.ser.PropertyFilter; | ||
import com.fasterxml.jackson.databind.ser.PropertyWriter; | ||
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.XmlTestBase; | ||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText; | ||
|
||
/** | ||
* Unit test for [PullRequest#616], problems with filtered serialization. | ||
*/ | ||
public class TestSerializationWithFilter extends XmlTestBase | ||
{ | ||
@JsonFilter("filter") | ||
static class Item | ||
{ | ||
@JacksonXmlText | ||
public int a; | ||
public int b; | ||
public int c; | ||
} | ||
|
||
public void testPullRequest616() throws Exception | ||
{ | ||
Item bean = new Item(); | ||
bean.a = 0; | ||
bean.b = 10; | ||
bean.c = 100; | ||
|
||
String exp = "<Item><b>10</b><c>100</c></Item>"; | ||
|
||
XmlMapper xmlMapper = new XmlMapper(); | ||
PropertyFilter filter = new SimpleBeanPropertyFilter() { | ||
@Override | ||
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception | ||
{ | ||
if (include(writer) && writer.getName().equals("a")) { | ||
int a = ((Item) pojo).a; | ||
if (a <= 0) | ||
return; | ||
} | ||
super.serializeAsField(pojo, jgen, provider, writer); | ||
} | ||
}; | ||
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("filter", filter); | ||
xmlMapper.setFilterProvider(filterProvider); | ||
String act = xmlMapper.writeValueAsString(bean); | ||
assertEquals(exp, act); | ||
} | ||
} |