Skip to content

Commit 98e0dfe

Browse files
cowtowncoderalex-bel-apica
authored andcommitted
Fix FasterXML#397 (alas, no unit tests)
# Conflicts: # release-notes/VERSION-2.x
1 parent 80becf3 commit 98e0dfe

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/FromXmlParser.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,16 @@ public JsonToken nextToken() throws IOException
462462
{
463463
JsonToken t = nextToken0();
464464
if (t != null) {
465+
final String loc = _parsingContext.pathAsPointer().toString();
465466
switch (t) {
466467
case FIELD_NAME:
467-
System.out.println("FromXmlParser.nextToken(): JsonToken.FIELD_NAME '"+_parsingContext.getCurrentName()+"'");
468+
System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.FIELD_NAME '%s'\n", loc, _parsingContext.getCurrentName());
468469
break;
469470
case VALUE_STRING:
470-
System.out.println("FromXmlParser.nextToken(): JsonToken.VALUE_STRING '"+getText()+"'");
471+
System.out.printf("FromXmlParser.nextToken() at '%s': JsonToken.VALUE_STRING '%s'\n", loc, getText());
471472
break;
472473
default:
473-
System.out.println("FromXmlParser.nextToken(): "+t);
474+
System.out.printf("FromXmlParser.nextToken() at '%s': %s\n", loc, t);
474475
}
475476
}
476477
return t;
@@ -486,6 +487,7 @@ public JsonToken nextToken() throws IOException
486487
JsonToken t = _nextToken;
487488
_currToken = t;
488489
_nextToken = null;
490+
489491
switch (t) {
490492
case START_OBJECT:
491493
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
@@ -501,7 +503,9 @@ public JsonToken nextToken() throws IOException
501503
_parsingContext.setCurrentName(_xmlTokens.getLocalName());
502504
break;
503505
default: // VALUE_STRING, VALUE_NULL
504-
// should be fine as is?
506+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index` anyway; not
507+
// used for Object contexts, updated automatically by "createChildXxxContext"
508+
_parsingContext.valueStarted();
505509
}
506510
return t;
507511
}
@@ -564,6 +568,8 @@ public JsonToken nextToken() throws IOException
564568
}
565569
// 07-Sep-2019, tatu: for [dataformat-xml#353], must NOT return second null
566570
if (_currToken != JsonToken.VALUE_NULL) {
571+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
572+
_parsingContext.valueStarted();
567573
return (_currToken = JsonToken.VALUE_NULL);
568574
}
569575
}
@@ -584,6 +590,8 @@ public JsonToken nextToken() throws IOException
584590
return (_currToken = JsonToken.FIELD_NAME);
585591
case XmlTokenStream.XML_ATTRIBUTE_VALUE:
586592
_currText = _xmlTokens.getText();
593+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
594+
_parsingContext.valueStarted();
587595
return (_currToken = JsonToken.VALUE_STRING);
588596
case XmlTokenStream.XML_TEXT:
589597
_currText = _xmlTokens.getText();
@@ -666,6 +674,8 @@ public String nextTextValue() throws IOException
666674

667675
// expected case; yes, got a String
668676
if (t == JsonToken.VALUE_STRING) {
677+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
678+
_parsingContext.valueStarted();
669679
return _currText;
670680
}
671681
_updateState(t);
@@ -715,6 +725,8 @@ public String nextTextValue() throws IOException
715725
// NOTE: this is different from nextToken() -- produce "", NOT null
716726
_mayBeLeaf = false;
717727
_currToken = JsonToken.VALUE_STRING;
728+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
729+
_parsingContext.valueStarted();
718730
return (_currText = "");
719731
}
720732
_currToken = _parsingContext.inArray() ? JsonToken.END_ARRAY : JsonToken.END_OBJECT;
@@ -735,6 +747,8 @@ public String nextTextValue() throws IOException
735747
break;
736748
case XmlTokenStream.XML_ATTRIBUTE_VALUE:
737749
_currToken = JsonToken.VALUE_STRING;
750+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
751+
_parsingContext.valueStarted();
738752
return (_currText = _xmlTokens.getText());
739753
case XmlTokenStream.XML_TEXT:
740754
_currText = _xmlTokens.getText();
@@ -748,6 +762,8 @@ public String nextTextValue() throws IOException
748762
}
749763
// NOTE: this is different from nextToken() -- NO work-around
750764
// for otherwise empty List/array
765+
// 13-May-2020, tatu: [dataformat-xml#397]: advance `index`
766+
_parsingContext.valueStarted();
751767
_currToken = JsonToken.VALUE_STRING;
752768
return _currText;
753769
}

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlReadContext.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static XmlReadContext createRootContext() {
105105

106106
public final XmlReadContext createChildArrayContext(int lineNr, int colNr)
107107
{
108+
++_index; // not needed for Object, but does not hurt so no need to check curr type
108109
XmlReadContext ctxt = _child;
109110
if (ctxt == null) {
110111
_child = ctxt = new XmlReadContext(this, TYPE_ARRAY, lineNr, colNr);
@@ -116,6 +117,7 @@ public final XmlReadContext createChildArrayContext(int lineNr, int colNr)
116117

117118
public final XmlReadContext createChildObjectContext(int lineNr, int colNr)
118119
{
120+
++_index; // not needed for Object, but does not hurt so no need to check curr type
119121
XmlReadContext ctxt = _child;
120122
if (ctxt == null) {
121123
_child = ctxt = new XmlReadContext(this, TYPE_OBJECT, lineNr, colNr);
@@ -127,7 +129,7 @@ public final XmlReadContext createChildObjectContext(int lineNr, int colNr)
127129

128130
/*
129131
/**********************************************************
130-
/* Abstract method implementation
132+
/* Abstract method implementation, overrides
131133
/**********************************************************
132134
*/
133135

@@ -140,18 +142,16 @@ public final XmlReadContext createChildObjectContext(int lineNr, int colNr)
140142
@Override
141143
public final XmlReadContext getParent() { return _parent; }
142144

143-
/*
144-
/**********************************************************
145-
/* State changes
146-
/**********************************************************
145+
/**
146+
* @return Location pointing to the point where the context
147+
* start marker was found
147148
*/
149+
@Override
150+
public final JsonLocation getStartLocation(Object srcRef) {
151+
// We don't keep track of offsets at this level (only reader does)
152+
long totalChars = -1L;
148153

149-
public final boolean expectComma() {
150-
throw new UnsupportedOperationException();
151-
}
152-
153-
public void setCurrentName(String name) {
154-
_currentName = name;
154+
return new JsonLocation(srcRef, totalChars, _lineNr, _columnNr);
155155
}
156156

157157
/*
@@ -161,15 +161,17 @@ public void setCurrentName(String name) {
161161
*/
162162

163163
/**
164-
* @return Location pointing to the point where the context
165-
* start marker was found
164+
* Method called to mark start of new value, mostly to update `index`
165+
* for Array and Root contexts.
166+
*
167+
* @since 2.12
166168
*/
167-
@Override
168-
public final JsonLocation getStartLocation(Object srcRef) {
169-
// We don't keep track of offsets at this level (only reader does)
170-
long totalChars = -1L;
169+
public final void valueStarted() {
170+
++_index;
171+
}
171172

172-
return new JsonLocation(srcRef, totalChars, _lineNr, _columnNr);
173+
public void setCurrentName(String name) {
174+
_currentName = name;
173175
}
174176

175177
public void setNamesToWrap(Set<String> namesToWrap) {

src/test/java/com/fasterxml/jackson/dataformat/xml/lists/ListDeser393Test.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.util.*;
44

5-
import com.fasterxml.jackson.annotation.JsonInclude;
6-
75
import com.fasterxml.jackson.databind.ObjectMapper;
86

97
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
@@ -12,20 +10,6 @@
1210

1311
public class ListDeser393Test extends XmlTestBase
1412
{
15-
@JacksonXmlRootElement(localName = "result")
16-
@JsonInclude(JsonInclude.Include.NON_NULL)
17-
static class Value393 {
18-
private Prices393 prices = new Prices393();
19-
20-
public void setPrices(Prices393 prices) {
21-
this.prices = prices;
22-
}
23-
24-
public Prices393 getPrices() {
25-
return this.prices;
26-
}
27-
}
28-
2913
@JacksonXmlRootElement(localName = "prices")
3014
static class Prices393 {
3115
private List<Price393> price = new ArrayList<Price393>();

0 commit comments

Comments
 (0)