File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed
main/java/com/fasterxml/jackson/dataformat/xml/deser
test/java/com/fasterxml/jackson/dataformat/xml/deser Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,19 @@ public enum Feature implements FormatFeature
86
86
*/
87
87
EMPTY_ELEMENT_AS_NULL (false ),
88
88
89
+ /**
90
+ * Feature that indicates whether XML Empty elements (ones where there are
91
+ * no separate start and end tags, but just one tag that ends with "/>")
92
+ * are exposed as {@link JsonToken#START_ARRAY} {@link JsonToken#END_ARRAY}) or not. If they are not
93
+ * returned as `[]` tokens, they will be returned as {@link JsonToken#VALUE_STRING}
94
+ * tokens with textual value of "" (empty String).
95
+ *<p>
96
+ * Default setting is {@code false}
97
+ *
98
+ * @since 2.9
99
+ */
100
+ EMPTY_ELEMENT_AS_EMPTY_ARRAY (false ),
101
+
89
102
/**
90
103
* Feature that indicates whether XML Schema Instance attribute
91
104
* {@code xsi:nil} will be processed automatically -- to indicate {@code null}
Original file line number Diff line number Diff line change 5
5
import javax .xml .XMLConstants ;
6
6
import javax .xml .stream .*;
7
7
8
+ import com .fasterxml .jackson .core .JsonToken ;
8
9
import org .codehaus .stax2 .XMLStreamLocation2 ;
9
10
import org .codehaus .stax2 .XMLStreamReader2 ;
10
11
@@ -549,6 +550,7 @@ private final int _next() throws XMLStreamException
549
550
550
551
/**
551
552
* @return Collected text, if any, EXCEPT that if {@code FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL}
553
+ * OR {@code FromXmlParser.Feature.EMPTY_ELEMENT_AS_EMPTY_ARRAY}
552
554
* AND empty element, returns {@code null}
553
555
*/
554
556
private final String _collectUntilTag () throws XMLStreamException
@@ -559,6 +561,9 @@ private final String _collectUntilTag() throws XMLStreamException
559
561
if (FromXmlParser .Feature .EMPTY_ELEMENT_AS_NULL .enabledIn (_formatFeatures )) {
560
562
return null ;
561
563
}
564
+ if (FromXmlParser .Feature .EMPTY_ELEMENT_AS_EMPTY_ARRAY .enabledIn (_formatFeatures )) {
565
+ return JsonToken .START_ARRAY .asString () + JsonToken .END_ARRAY .asString ();
566
+ }
562
567
return "" ;
563
568
}
564
569
Original file line number Diff line number Diff line change @@ -85,6 +85,26 @@ public void testEmptyElement() throws Exception
85
85
assertEquals ("" , name .last );
86
86
}
87
87
88
+ public void testEmptyElementEmptyArray () throws Exception
89
+ {
90
+ final String XML = "<name><first/><last></last></name>" ;
91
+
92
+ // Default settings (since 2.12): empty element does NOT become `null`:
93
+ Name name = MAPPER .readValue (XML , Name .class );
94
+ assertNotNull (name );
95
+ assertEquals ("" , name .first );
96
+ assertEquals ("" , name .last );
97
+
98
+ // but can be changed
99
+ XmlMapper mapper2 = XmlMapper .builder ()
100
+ .enable (FromXmlParser .Feature .EMPTY_ELEMENT_AS_EMPTY_ARRAY )
101
+ .build ();
102
+ name = mapper2 .readValue (XML , Name .class );
103
+ assertNotNull (name );
104
+ assertEquals ("[]" , name .first );
105
+ assertEquals ("" , name .last );
106
+ }
107
+
88
108
public void testEmptyStringElement () throws Exception
89
109
{
90
110
// then with empty element
You can’t perform that action at this time.
0 commit comments