Skip to content

Commit 51ad218

Browse files
committed
Refactoring/cleanup to prepare for #561
1 parent f38940f commit 51ad218

File tree

6 files changed

+42
-21
lines changed

6 files changed

+42
-21
lines changed

src/main/java/com/fasterxml/jackson/core/base/ParserBase.java

+20
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,26 @@ protected void _throwUnquotedSpace(int i, String ctxtDesc) throws JsonParseExcep
10471047
}
10481048
}
10491049

1050+
/**
1051+
* @return Description to use as "valid tokens" in an exception message about
1052+
* invalid (unrecognized) token
1053+
*
1054+
* @since 2.10
1055+
*/
1056+
protected String _validJsonTokenList() throws IOException {
1057+
return "('null', 'true', 'false' or NaN)";
1058+
}
1059+
1060+
/**
1061+
* @return Description to use as "valid JSON values" in an exception message about
1062+
* invalid (unrecognized) JSON value
1063+
*
1064+
* @since 2.10
1065+
*/
1066+
protected String _validJsonValueList() throws IOException {
1067+
return "(number, String, array, object, 'true', 'false' or 'null')";
1068+
}
1069+
10501070
/*
10511071
/**********************************************************
10521072
/* Base64 handling support

src/main/java/com/fasterxml/jackson/core/json/ReaderBasedJsonParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1910,10 +1910,10 @@ protected JsonToken _handleOddValue(int i) throws IOException
19101910
}
19111911
// [core#77] Try to decode most likely token
19121912
if (Character.isJavaIdentifierStart(i)) {
1913-
_reportInvalidToken(""+((char) i), "('true', 'false' or 'null')");
1913+
_reportInvalidToken(""+((char) i), _validJsonTokenList());
19141914
}
19151915
// but if it doesn't look like a token:
1916-
_reportUnexpectedChar(i, "expected a valid value (number, String, array, object, 'true', 'false' or 'null')");
1916+
_reportUnexpectedChar(i, "expected a valid value "+_validJsonValueList());
19171917
return null;
19181918
}
19191919

@@ -2842,7 +2842,7 @@ private final void _updateNameLocation()
28422842
*/
28432843

28442844
protected void _reportInvalidToken(String matchedPart) throws IOException {
2845-
_reportInvalidToken(matchedPart, "'null', 'true', 'false' or NaN");
2845+
_reportInvalidToken(matchedPart, _validJsonTokenList());
28462846
}
28472847

28482848
protected void _reportInvalidToken(String matchedPart, String msg) throws IOException

src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -2061,10 +2061,10 @@ protected JsonToken _handleUnexpectedValue(int c)
20612061
}
20622062
// [core#77] Try to decode most likely token
20632063
if (Character.isJavaIdentifierStart(c)) {
2064-
_reportInvalidToken(c, ""+((char) c), "('true', 'false' or 'null')");
2064+
_reportInvalidToken(c, ""+((char) c), _validJsonTokenList());
20652065
}
20662066
// but if it doesn't look like a token:
2067-
_reportUnexpectedChar(c, "expected a valid value (number, String, array, object, 'true', 'false' or 'null')");
2067+
_reportUnexpectedChar(c, "expected a valid value "+_validJsonValueList());
20682068
return null;
20692069
}
20702070

@@ -2673,7 +2673,7 @@ private final void _skipUtf8_4() throws IOException
26732673

26742674
protected void _reportInvalidToken(int ch, String matchedPart) throws IOException
26752675
{
2676-
_reportInvalidToken(ch, matchedPart, "'null', 'true', 'false' or NaN");
2676+
_reportInvalidToken(ch, matchedPart, _validJsonTokenList());
26772677
}
26782678

26792679
protected void _reportInvalidToken(int ch, String matchedPart, String msg)

src/main/java/com/fasterxml/jackson/core/json/UTF8StreamJsonParser.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -2648,10 +2648,10 @@ protected JsonToken _handleUnexpectedValue(int c) throws IOException
26482648
}
26492649
// [core#77] Try to decode most likely token
26502650
if (Character.isJavaIdentifierStart(c)) {
2651-
_reportInvalidToken(""+((char) c), "('true', 'false' or 'null')");
2651+
_reportInvalidToken(""+((char) c), _validJsonTokenList());
26522652
}
26532653
// but if it doesn't look like a token:
2654-
_reportUnexpectedChar(c, "expected a valid value (number, String, array, object, 'true', 'false' or 'null')");
2654+
_reportUnexpectedChar(c, "expected a valid value "+_validJsonValueList());
26552655
return null;
26562656
}
26572657

@@ -3525,11 +3525,11 @@ private int nextByte() throws IOException
35253525

35263526
protected void _reportInvalidToken(String matchedPart, int ptr) throws IOException {
35273527
_inputPtr = ptr;
3528-
_reportInvalidToken(matchedPart, "'null', 'true', 'false' or NaN");
3528+
_reportInvalidToken(matchedPart, _validJsonTokenList());
35293529
}
35303530

35313531
protected void _reportInvalidToken(String matchedPart) throws IOException {
3532-
_reportInvalidToken(matchedPart, "'null', 'true', 'false' or NaN");
3532+
_reportInvalidToken(matchedPart, _validJsonTokenList());
35333533
}
35343534

35353535
protected void _reportInvalidToken(String matchedPart, String msg) throws IOException

src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingJsonParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ protected JsonToken _startUnexpectedValue(boolean leadingComma, int ch) throws I
919919
return _finishNonStdToken(NON_STD_TOKEN_INFINITY, 1);
920920
}
921921
// !!! TODO: maybe try to collect more information for better diagnostics
922-
_reportUnexpectedChar(ch, "expected a valid value (number, String, array, object, 'true', 'false' or 'null')");
922+
_reportUnexpectedChar(ch, "expected a valid value "+_validJsonValueList());
923923
return null;
924924
}
925925

@@ -1263,10 +1263,10 @@ protected JsonToken _reportErrorToken(String actualToken) throws IOException
12631263
{
12641264
// !!! TODO: Include non-standard ones if enabled
12651265
_reportError("Unrecognized token '%s': was expecting %s", _textBuffer.contentsAsString(),
1266-
"'null', 'true' or 'false'");
1266+
_validJsonTokenList());
12671267
return JsonToken.NOT_AVAILABLE; // never gets here
12681268
}
1269-
1269+
12701270
/*
12711271
/**********************************************************************
12721272
/* Second-level decoding, Number decoding

src/test/java/com/fasterxml/jackson/core/json/TestMaxErrorSize.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void testLongErrorMessage() throws Exception
2020

2121
public void testLongErrorMessageReader() throws Exception
2222
{
23-
_testLongErrorMessage(MODE_READER);
23+
_testLongErrorMessage(MODE_READER);
2424
}
2525

2626
private void _testLongErrorMessage(int mode) throws Exception
@@ -36,9 +36,10 @@ private void _testLongErrorMessage(int mode) throws Exception
3636
fail("Expected an exception for unrecognized token");
3737
} catch (JsonParseException jpe) {
3838
String msg = jpe.getMessage();
39-
final String expectedPrefix = "Unrecognized token '";
40-
final String expectedSuffix = "...': was expecting ('true', 'false' or 'null')";
41-
assertTrue(msg.startsWith(expectedPrefix));
39+
final String expectedPrefix = "Unrecognized token '";
40+
final String expectedSuffix = "...': was expecting";
41+
verifyException(jpe, expectedPrefix);
42+
verifyException(jpe, expectedSuffix);
4243
assertTrue(msg.contains(expectedSuffix));
4344
int tokenLen = msg.indexOf (expectedSuffix) - expectedPrefix.length();
4445
assertEquals(EXPECTED_MAX_TOKEN_LEN, tokenLen);
@@ -65,10 +66,10 @@ public void _testShortErrorMessage(int mode) throws Exception
6566
} catch (JsonParseException jpe) {
6667
String msg = jpe.getMessage();
6768
final String expectedPrefix = "Unrecognized token '";
68-
final String expectedSuffix = "': was expecting ('true', 'false' or 'null')";
69-
assertTrue(msg.startsWith(expectedPrefix));
70-
assertTrue(msg.contains(expectedSuffix));
71-
int tokenLen = msg.indexOf (expectedSuffix) - expectedPrefix.length();
69+
final String expectedSuffix = "': was expecting";
70+
verifyException(jpe, expectedPrefix);
71+
verifyException(jpe, expectedSuffix);
72+
int tokenLen = msg.indexOf(expectedSuffix) - expectedPrefix.length();
7273
assertEquals(DOC.length(), tokenLen);
7374
}
7475
jp.close();

0 commit comments

Comments
 (0)