@@ -110,6 +110,15 @@ private Feature(boolean defaultState) {
110
110
*/
111
111
protected Event _lastEvent ;
112
112
113
+ /**
114
+ * To keep track of tags ("type ids"), need to either get tags for all
115
+ * events, or, keep tag of relevant event that might have it: this is
116
+ * different from {@code _lastEvent} in some cases.
117
+ *
118
+ * @since 2.12
119
+ */
120
+ protected Event _lastTagEvent ;
121
+
113
122
/**
114
123
* We need to keep track of text values.
115
124
*/
@@ -139,7 +148,7 @@ private Feature(boolean defaultState) {
139
148
* structured types, value whose first token current token is.
140
149
*/
141
150
protected String _currentAnchor ;
142
-
151
+
143
152
/*
144
153
/**********************************************************************
145
154
/* Life-cycle
@@ -387,6 +396,7 @@ public JsonToken nextToken() throws IOException
387
396
// is null ok? Assume it is, for now, consider to be same as end-of-doc
388
397
if (evt == null ) {
389
398
_currentAnchor = null ;
399
+ _lastTagEvent = null ;
390
400
return (_currToken = null );
391
401
}
392
402
_lastEvent = evt ;
@@ -397,6 +407,7 @@ public JsonToken nextToken() throws IOException
397
407
if (_currToken != JsonToken .FIELD_NAME ) {
398
408
if (!evt .is (Event .ID .Scalar )) {
399
409
_currentAnchor = null ;
410
+ _lastTagEvent = null ;
400
411
// end is fine
401
412
if (evt .is (Event .ID .MappingEnd )) {
402
413
if (!_parsingContext .inObject ()) { // sanity check is optional, but let's do it for now
@@ -407,6 +418,7 @@ public JsonToken nextToken() throws IOException
407
418
}
408
419
_reportError ("Expected a field name (Scalar value in YAML), got this instead: " +evt );
409
420
}
421
+
410
422
// 20-Feb-2019, tatu: [dataformats-text#123] Looks like YAML exposes Anchor for Object at point
411
423
// where we return START_OBJECT (which makes sense), but, alas, Jackson expects that at point
412
424
// after first FIELD_NAME. So we will need to defer clearing of the anchor slightly,
@@ -415,9 +427,15 @@ public JsonToken nextToken() throws IOException
415
427
// test case given.
416
428
final ScalarEvent scalar = (ScalarEvent ) evt ;
417
429
final String newAnchor = scalar .getAnchor ();
418
- if ((newAnchor != null ) || (_currToken != JsonToken .START_OBJECT )) {
430
+ final boolean firstEntry = (_currToken == JsonToken .START_OBJECT );
431
+ if ((newAnchor != null ) || !firstEntry ) {
419
432
_currentAnchor = scalar .getAnchor ();
420
433
}
434
+ // 23-Nov-2020, tatu: [dataformats-text#232] shows case where ref to type id
435
+ // needs to be similarly deferred...
436
+ if (!firstEntry ) {
437
+ _lastTagEvent = evt ;
438
+ }
421
439
final String name = scalar .getValue ();
422
440
_currentFieldName = name ;
423
441
_parsingContext .setCurrentName (name );
@@ -429,6 +447,7 @@ public JsonToken nextToken() throws IOException
429
447
430
448
// Ugh. Why not expose id, to be able to Switch?
431
449
_currentAnchor = null ;
450
+ _lastTagEvent = evt ;
432
451
433
452
// scalar values are probably the commonest:
434
453
if (evt .is (Event .ID .Scalar )) {
@@ -1000,11 +1019,16 @@ public String getObjectId() throws IOException
1000
1019
public String getTypeId () throws IOException
1001
1020
{
1002
1021
String tag ;
1003
- if (_lastEvent instanceof CollectionStartEvent ) {
1004
- tag = ((CollectionStartEvent ) _lastEvent ).getTag ();
1005
- } else if (_lastEvent instanceof ScalarEvent ) {
1006
- tag = ((ScalarEvent ) _lastEvent ).getTag ();
1022
+
1023
+ if (_lastTagEvent instanceof CollectionStartEvent ) {
1024
+ tag = ((CollectionStartEvent ) _lastTagEvent ).getTag ();
1025
+ //System.err.println("getTypeId() at "+currentToken()+", last was collection ("+_lastTagEvent.getClass().getSimpleName()+") -> "+tag);
1026
+ } else if (_lastTagEvent instanceof ScalarEvent ) {
1027
+ tag = ((ScalarEvent ) _lastTagEvent ).getTag ();
1028
+ //System.err.println("getTypeId() at "+currentToken()+", last was scalar -> "+tag+", scalar == "+_lastEvent);
1029
+
1007
1030
} else {
1031
+ //System.err.println("getTypeId(), something else, curr token: "+currentToken());
1008
1032
return null ;
1009
1033
}
1010
1034
if (tag != null ) {
0 commit comments