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
8686 */
8787 EMPTY_ELEMENT_AS_NULL (false ),
8888
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+
89102 /**
90103 * Feature that indicates whether XML Schema Instance attribute
91104 * {@code xsi:nil} will be processed automatically -- to indicate {@code null}
Original file line number Diff line number Diff line change 55import javax .xml .XMLConstants ;
66import javax .xml .stream .*;
77
8+ import com .fasterxml .jackson .core .JsonToken ;
89import org .codehaus .stax2 .XMLStreamLocation2 ;
910import org .codehaus .stax2 .XMLStreamReader2 ;
1011
@@ -549,6 +550,7 @@ private final int _next() throws XMLStreamException
549550
550551 /**
551552 * @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}
552554 * AND empty element, returns {@code null}
553555 */
554556 private final String _collectUntilTag () throws XMLStreamException
@@ -559,6 +561,9 @@ private final String _collectUntilTag() throws XMLStreamException
559561 if (FromXmlParser .Feature .EMPTY_ELEMENT_AS_NULL .enabledIn (_formatFeatures )) {
560562 return null ;
561563 }
564+ if (FromXmlParser .Feature .EMPTY_ELEMENT_AS_EMPTY_ARRAY .enabledIn (_formatFeatures )) {
565+ return JsonToken .START_ARRAY .asString () + JsonToken .END_ARRAY .asString ();
566+ }
562567 return "" ;
563568 }
564569
Original file line number Diff line number Diff line change @@ -85,6 +85,26 @@ public void testEmptyElement() throws Exception
8585 assertEquals ("" , name .last );
8686 }
8787
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+
88108 public void testEmptyStringElement () throws Exception
89109 {
90110 // then with empty element
You can’t perform that action at this time.
0 commit comments