Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make empty tags configurable during serialization #640

Open
wants to merge 6 commits into
base: 2.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public enum Feature implements FormatFeature
*/
EMPTY_ELEMENT_AS_NULL(false),

/**
* Feature that indicates whether XML Empty elements (ones where there are
* no separate start and end tags, but just one tag that ends with "/>")
* are exposed as {@link JsonToken#START_ARRAY} {@link JsonToken#END_ARRAY}) or not. If they are not
* returned as `[]` tokens, they will be returned as {@link JsonToken#VALUE_STRING}
* tokens with textual value of "" (empty String).
*<p>
* Default setting is {@code false}
*
* @since 2.9
*/
EMPTY_ELEMENT_AS_EMPTY_ARRAY(false),

/**
* Feature that indicates whether XML Schema Instance attribute
* {@code xsi:nil} will be processed automatically -- to indicate {@code null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.xml.XMLConstants;
import javax.xml.stream.*;

import com.fasterxml.jackson.core.JsonToken;
import org.codehaus.stax2.XMLStreamLocation2;
import org.codehaus.stax2.XMLStreamReader2;

Expand Down Expand Up @@ -549,6 +550,7 @@ private final int _next() throws XMLStreamException

/**
* @return Collected text, if any, EXCEPT that if {@code FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL}
* OR {@code FromXmlParser.Feature.EMPTY_ELEMENT_AS_EMPTY_ARRAY}
Croway marked this conversation as resolved.
Show resolved Hide resolved
* AND empty element, returns {@code null}
*/
private final String _collectUntilTag() throws XMLStreamException
Expand All @@ -559,6 +561,9 @@ private final String _collectUntilTag() throws XMLStreamException
if (FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL.enabledIn(_formatFeatures)) {
return null;
}
if (FromXmlParser.Feature.EMPTY_ELEMENT_AS_EMPTY_ARRAY.enabledIn(_formatFeatures)) {
return JsonToken.START_ARRAY.asString() + JsonToken.END_ARRAY.asString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how this makes sense -- all we get here is String "[]", which won't be further decoded?

}
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ public void testEmptyElement() throws Exception
assertEquals("", name.last);
}

public void testEmptyElementEmptyArray() throws Exception
{
final String XML = "<name><first/><last></last></name>";

// Default settings (since 2.12): empty element does NOT become `null`:
Name name = MAPPER.readValue(XML, Name.class);
assertNotNull(name);
assertEquals("", name.first);
assertEquals("", name.last);

// but can be changed
XmlMapper mapper2 = XmlMapper.builder()
.enable(FromXmlParser.Feature.EMPTY_ELEMENT_AS_EMPTY_ARRAY)
.build();
name = mapper2.readValue(XML, Name.class);
assertNotNull(name);
assertEquals("[]", name.first);
assertEquals("", name.last);
}

public void testEmptyStringElement() throws Exception
{
// then with empty element
Expand Down