Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 1, 2025
2 parents d54fabe + 27c1215 commit 9d9619c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ a pure JSON library.
(contributed by Eduard G)
#1361: `JsonPointer` parsing of '~' not followed by "0" or "1" unexpected
(reported by @slz30)
#1394: Wrong/misleading error for "extra" close token (`]` or `}`) when
at root level

2.18.3 (not yet released)

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/tools/jackson/core/json/JsonParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,25 @@ protected char _handleUnrecognizedCharacterEscape(char ch) throws StreamReadExce

// Promoted from `ParserBase` in 3.0
protected void _reportMismatchedEndMarker(int actCh, char expCh) throws StreamReadException {
TokenStreamContext ctxt = streamReadContext();
final TokenStreamContext ctxt = streamReadContext();
// 31-Jan-2025, tatu: [core#1394] Need to check case of no open scope
if (ctxt.inRoot()) {
_reportExtraEndMarker(actCh);
return;
}
final String msg = String.format(
"Unexpected close marker '%s': expected '%c' (for %s starting at %s)",
(char) actCh, expCh, ctxt.typeDesc(), ctxt.startLocation(_contentReference()));
throw _constructReadException(msg, _currentLocationMinusOne());
}

protected void _reportExtraEndMarker(int actCh) throws StreamReadException {
final String scopeDesc = (actCh == '}') ? "Object" : "Array";
final String msg = String.format(
"Unexpected close marker '%s': no open %s to close", (char) actCh, scopeDesc);
throw _constructReadException(msg, _currentLocationMinusOne());
}

// Method called to report a problem with unquoted control character.
// Note: it is possible to suppress some instances of
// exception by enabling {@link JsonReadFeature#ALLOW_UNESCAPED_CONTROL_CHARS}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import org.junit.jupiter.api.Test;

import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonParser;
import tools.jackson.core.JsonToken;
import tools.jackson.core.*;
import tools.jackson.core.exc.StreamReadException;
import tools.jackson.core.unittest.*;

Expand Down Expand Up @@ -189,4 +187,50 @@ private void _testMisssingColon(int mode)
}
p.close();
}

// [core#1394]
@Test
void extraEndArray() throws Exception
{
for (int mode : ALL_MODES) {
_extraEndArray(mode);
}
}

public void _extraEndArray(int mode) throws Exception
{
try (JsonParser p = createParser(mode, "{ }]")) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.END_OBJECT, p.nextToken());
try {
p.nextToken();
fail("Should have thrown an exception");
} catch (StreamReadException e) {
verifyException(e, "Unexpected close marker ']': no open Array");
}
}
}

// [core#1394]
@Test
void extraEndObject() throws Exception
{
for (int mode : ALL_MODES) {
_extraEndObject(mode);
}
}

public void _extraEndObject(int mode) throws Exception
{
try (JsonParser p = createParser(mode, "[ ]}")) {
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Should have thrown an exception");
} catch (StreamReadException e) {
verifyException(e, "Unexpected close marker '}': no open Object");
}
}
}
}

0 comments on commit 9d9619c

Please sign in to comment.