Skip to content

Commit

Permalink
Fixed #226
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 5, 2020
1 parent 07693ee commit c25683e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project: jackson-dataformat-xml
#205: `XmlMapper`/`UntypedObjectDeserializer` swallows duplicated elements in
XML documents
(reported by joaovarandas@github)
#226: XML to JSON - IOException when parsing XML with XMLMapper
(reported by dbories@github)
#262: Make `ToXmlGenerator` non-final
(requested by Dave J)
#273: Input mismatch with case-insensitive properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,18 +630,33 @@ public JsonToken nextToken() throws IOException
// we had an empty String (or all white space), and we are
// deserializing an array, we better hide the empty text.
// Also: must skip following END_ELEMENT
_skipEndElement();
if (_parsingContext.inArray()) {
if (XmlTokenStream._allWs(_currText)) {
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
// expose as empty Object, not null (or, worse, as used to
// be done, by swallowing the token)
_nextToken = JsonToken.END_OBJECT;
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
return (_currToken = JsonToken.START_OBJECT);
// 05-Jun-2020, tatu: ... if there is one; we may actually alternatively
// get START_ELEMENT for "mixed content" case; if so, need to change to
// expose "XmlText" as separate property
token = _nextToken();

if (token == XmlTokenStream.XML_END_ELEMENT) {
if (_parsingContext.inArray()) {
if (XmlTokenStream._allWs(_currText)) {
// 06-Jan-2015, tatu: as per [dataformat-xml#180], need to
// expose as empty Object, not null (or, worse, as used to
// be done, by swallowing the token)
_nextToken = JsonToken.END_OBJECT;
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
return (_currToken = JsonToken.START_OBJECT);
}
}
return (_currToken = JsonToken.VALUE_STRING);
}
return (_currToken = JsonToken.VALUE_STRING);
if (token != XmlTokenStream.XML_START_ELEMENT) {
throw new JsonParseException(this, String.format(
"Internal error: Expected END_ELEMENT (%d) or START_ELEMENT (%d), got event of type %d",
XmlTokenStream.XML_END_ELEMENT, XmlTokenStream.XML_START_ELEMENT, token));
}
// fall-through, except must create new context AND push back
// START_ELEMENT we just saw:
_xmlTokens.pushbackCurrentToken();
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
}
// [dataformat-xml#177]: empty text may also need to be skipped
// but... [dataformat-xml#191]: looks like we can't short-cut, must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,16 @@ public class XmlTokenStream
protected String _namespaceURI;

protected String _textValue;


/**
* Marker flag set if caller wants to "push back" current token so
* that next call to {@link #next()} should simply be given what was
* already read.
*
* @since 2.12
*/
protected boolean _repeatCurrentToken;

/*
/**********************************************************************
/* State for handling virtual wrapping
Expand Down Expand Up @@ -203,6 +212,10 @@ private String _loc() {
// public int next0() throws XMLStreamException
public int next() throws XMLStreamException
{
if (_repeatCurrentToken) {
_repeatCurrentToken = false;
return _currentState;
}
if (_repeatElement != 0) {
return (_currentState = _handleRepeatElement());
}
Expand Down Expand Up @@ -292,6 +305,17 @@ protected void repeatStartElement()
_repeatElement = REPLAY_START_DUP;
}

/**
* Method that can be called to ask stream to literally just return current token
* with the next call to {@link #next()}, without more work.
*
* @since 2.12
*/
protected void pushbackCurrentToken()
{
_repeatCurrentToken = true;
}

/**
* Method called to skip any attributes current START_ELEMENT may have,
* so that they are not returned as token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public void testMultipleMixedContent() throws Exception
a2q("{'':['first','second','third'],'a':'1','b':'2'}")),
XML_MAPPER.readTree("<root>first<a>1</a>second<b>2</b>third</root>"));
}

// [dataformat-xml#226]
public void testMixed226() throws Exception
{
final String XML = "<root>\n"
+"<a>mixed1 <b>leaf</b>"
+" mixed2</a>\n"
+"</root>";
assertEquals(JSON_MAPPER.readTree(
a2q("{'a':{'':['mixed1 ',' mixed2'],'b':'leaf'}}")),
XML_MAPPER.readTree(XML));
}
}

This file was deleted.

0 comments on commit c25683e

Please sign in to comment.