diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java index 181fc7b81..b3535a7b8 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java @@ -462,15 +462,16 @@ public JsonToken nextToken() throws IOException { JsonToken t = nextToken0(); if (t != null) { + final String loc = _parsingContext.pathAsPointer().toString(); switch (t) { case FIELD_NAME: - System.out.println("FromXmlParser.nextToken(): JsonToken.FIELD_NAME '"+_parsingContext.getCurrentName()+"'"); + System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.FIELD_NAME '%s'\n", loc, _parsingContext.getCurrentName()); break; case VALUE_STRING: - System.out.println("FromXmlParser.nextToken(): JsonToken.VALUE_STRING '"+getText()+"'"); + System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.VALUE_STRING '%s'\n", loc, getText()); break; default: - System.out.println("FromXmlParser.nextToken(): "+t); + System.out.printf("FromXmlParser.nextToken() at '%s': %s\n", loc, t); } } return t; @@ -486,6 +487,7 @@ public JsonToken nextToken() throws IOException JsonToken t = _nextToken; _currToken = t; _nextToken = null; + switch (t) { case START_OBJECT: _parsingContext = _parsingContext.createChildObjectContext(-1, -1); @@ -501,7 +503,9 @@ public JsonToken nextToken() throws IOException _parsingContext.setCurrentName(_xmlTokens.getLocalName()); break; default: // VALUE_STRING, VALUE_NULL - // should be fine as is? + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` anyway; not + // used for Object contexts, updated automatically by "createChildXxxContext" + _parsingContext.valueStarted(); } return t; } @@ -564,6 +568,8 @@ public JsonToken nextToken() throws IOException } // 07-Sep-2019, tatu: for [dataformat-xml#353], must NOT return second null if (_currToken != JsonToken.VALUE_NULL) { + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` + _parsingContext.valueStarted(); return (_currToken = JsonToken.VALUE_NULL); } } @@ -584,6 +590,8 @@ public JsonToken nextToken() throws IOException return (_currToken = JsonToken.FIELD_NAME); case XmlTokenStream.XML_ATTRIBUTE_VALUE: _currText = _xmlTokens.getText(); + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` + _parsingContext.valueStarted(); return (_currToken = JsonToken.VALUE_STRING); case XmlTokenStream.XML_TEXT: _currText = _xmlTokens.getText(); @@ -666,6 +674,8 @@ public String nextTextValue() throws IOException // expected case; yes, got a String if (t == JsonToken.VALUE_STRING) { + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` + _parsingContext.valueStarted(); return _currText; } _updateState(t); @@ -715,6 +725,8 @@ public String nextTextValue() throws IOException // NOTE: this is different from nextToken() -- produce "", NOT null _mayBeLeaf = false; _currToken = JsonToken.VALUE_STRING; + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` + _parsingContext.valueStarted(); return (_currText = ""); } _currToken = _parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT; @@ -735,6 +747,8 @@ public String nextTextValue() throws IOException break; case XmlTokenStream.XML_ATTRIBUTE_VALUE: _currToken = JsonToken.VALUE_STRING; + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` + _parsingContext.valueStarted(); return (_currText = _xmlTokens.getText()); case XmlTokenStream.XML_TEXT: _currText = _xmlTokens.getText(); @@ -748,6 +762,8 @@ public String nextTextValue() throws IOException } // NOTE: this is different from nextToken() -- NO work-around // for otherwise empty List/array + // 13-May-2020, tatu: [dataformat-xml#397]: advance `index` + _parsingContext.valueStarted(); _currToken = JsonToken.VALUE_STRING; return _currText; } diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlReadContext.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlReadContext.java index 5562a59f5..befc28e79 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlReadContext.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlReadContext.java @@ -105,6 +105,7 @@ public static XmlReadContext createRootContext() { public final XmlReadContext createChildArrayContext(int lineNr, int colNr) { + ++_index; // not needed for Object, but does not hurt so no need to check curr type XmlReadContext ctxt = _child; if (ctxt == null) { _child = ctxt = new XmlReadContext(this, TYPE_ARRAY, lineNr, colNr); @@ -116,6 +117,7 @@ public final XmlReadContext createChildArrayContext(int lineNr, int colNr) public final XmlReadContext createChildObjectContext(int lineNr, int colNr) { + ++_index; // not needed for Object, but does not hurt so no need to check curr type XmlReadContext ctxt = _child; if (ctxt == null) { _child = ctxt = new XmlReadContext(this, TYPE_OBJECT, lineNr, colNr); @@ -127,7 +129,7 @@ public final XmlReadContext createChildObjectContext(int lineNr, int colNr) /* /********************************************************** - /* Abstract method implementation + /* Abstract method implementation, overrides /********************************************************** */ @@ -140,18 +142,16 @@ public final XmlReadContext createChildObjectContext(int lineNr, int colNr) @Override public final XmlReadContext getParent() { return _parent; } - /* - /********************************************************** - /* State changes - /********************************************************** + /** + * @return Location pointing to the point where the context + * start marker was found */ + @Override + public final JsonLocation getStartLocation(Object srcRef) { + // We don't keep track of offsets at this level (only reader does) + long totalChars = -1L; - public final boolean expectComma() { - throw new UnsupportedOperationException(); - } - - public void setCurrentName(String name) { - _currentName = name; + return new JsonLocation(srcRef, totalChars, _lineNr, _columnNr); } /* @@ -161,15 +161,17 @@ public void setCurrentName(String name) { */ /** - * @return Location pointing to the point where the context - * start marker was found + * Method called to mark start of new value, mostly to update `index` + * for Array and Root contexts. + * + * @since 2.12 */ - @Override - public final JsonLocation getStartLocation(Object srcRef) { - // We don't keep track of offsets at this level (only reader does) - long totalChars = -1L; + public final void valueStarted() { + ++_index; + } - return new JsonLocation(srcRef, totalChars, _lineNr, _columnNr); + public void setCurrentName(String name) { + _currentName = name; } public void setNamesToWrap(Set namesToWrap) { diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/ListDeser393Test.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/ListDeser393Test.java index eec836a1a..12dd9515d 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/ListDeser393Test.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/lists/ListDeser393Test.java @@ -2,8 +2,6 @@ import java.util.*; -import com.fasterxml.jackson.annotation.JsonInclude; - import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.xml.XmlTestBase; @@ -12,20 +10,6 @@ public class ListDeser393Test extends XmlTestBase { - @JacksonXmlRootElement(localName = "result") - @JsonInclude(JsonInclude.Include.NON_NULL) - static class Value393 { - private Prices393 prices = new Prices393(); - - public void setPrices(Prices393 prices) { - this.prices = prices; - } - - public Prices393 getPrices() { - return this.prices; - } - } - @JacksonXmlRootElement(localName = "prices") static class Prices393 { private List price = new ArrayList();