Skip to content

Commit 28aec40

Browse files
committed
Fix #492
1 parent a24507e commit 28aec40

12 files changed

+74
-80
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ JSON library.
2121
#432: Add new `TreeNode` subtypes: `ArrayTreeNode`, `ObjectTreeNode`
2222
#433: Add Builder pattern for creating configured Stream factories
2323
#456: Add `JsonParser.readAsValue(ResolvedType)`
24+
#492: Ensure primitive type names in error message enclosed in backticks
2425
- Rename `JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT` as `AUTO_CLOSE_CONTENT`

src/main/java/com/fasterxml/jackson/core/JsonParser.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -1014,10 +1014,10 @@ public int getText(Writer writer) throws IOException, UnsupportedOperationExcept
10141014
public byte getByteValue() throws IOException {
10151015
int value = getIntValue();
10161016
// So far so good: but does it fit?
1017-
// [JACKSON-804]: Let's actually allow range of [-128, 255], as those are uniquely mapped
1018-
// (instead of just signed range of [-128, 127])
1017+
// Let's actually allow range of [-128, 255] instead of just signed range of [-128, 127]
1018+
// since "unsigned" usage quite common for bytes (but Java may use signed range, too)
10191019
if (value < MIN_BYTE_I || value > MAX_BYTE_I) {
1020-
throw _constructError("Numeric value ("+getText()+") out of range of Java byte");
1020+
throw _constructError("Numeric value (%s) out of range of `byte`", getText());
10211021
}
10221022
return (byte) value;
10231023
}
@@ -1039,7 +1039,7 @@ public short getShortValue() throws IOException
10391039
{
10401040
int value = getIntValue();
10411041
if (value < MIN_SHORT_I || value > MAX_SHORT_I) {
1042-
throw _constructError("Numeric value ("+getText()+") out of range of Java short");
1042+
throw _constructError("Numeric value (%s) out of range of `short`", getText());
10431043
}
10441044
return (short) value;
10451045
}
@@ -1523,6 +1523,16 @@ protected JsonParseException _constructError(String msg) {
15231523
.withRequestPayload(_requestPayload);
15241524
}
15251525

1526+
protected JsonParseException _constructError(String msg, Object arg) {
1527+
return new JsonParseException(this, String.format(msg, arg))
1528+
.withRequestPayload(_requestPayload);
1529+
}
1530+
1531+
protected JsonParseException _constructError(String msg, Object arg1, Object arg2) {
1532+
return new JsonParseException(this, String.format(msg, arg1, arg2))
1533+
.withRequestPayload(_requestPayload);
1534+
}
1535+
15261536
/**
15271537
* Helper method to call for operations that are not supported by
15281538
* parser implementation.

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,8 @@ public BigInteger getBigIntegerValue() throws IOException
633633
public float getFloatValue() throws IOException
634634
{
635635
double value = getDoubleValue();
636-
/* 22-Jan-2009, tatu: Bounds/range checks would be tricky
637-
* here, so let's not bother even trying...
638-
*/
636+
// 22-Jan-2009, tatu: Bounds/range checks would be tricky
637+
// here, so let's not bother even trying...
639638
/*
640639
if (value < -Float.MAX_VALUE || value > MAX_FLOAT_D) {
641640
_reportError("Numeric value ("+getText()+") out of range of Java float");
@@ -816,7 +815,7 @@ private void _parseSlowInt(int expType) throws IOException
816815
protected void _reportTooLongInt(int expType, String rawNum) throws IOException
817816
{
818817
final String numDesc = _longIntegerDesc(rawNum);
819-
_reportError("Numeric value (%s) out of range of %s", numDesc,
818+
_reportError("Numeric value (%s) out of range of `%s`", numDesc,
820819
(expType == NR_LONG) ? "long" : "int");
821820
}
822821

@@ -833,7 +832,7 @@ protected void convertNumberToInt() throws IOException
833832
// Let's verify it's lossless conversion by simple roundtrip
834833
int result = (int) _numberLong;
835834
if (((long) result) != _numberLong) {
836-
_reportError("Numeric value ("+getText()+") out of range of int");
835+
_reportError("Numeric value ("+getText()+") out of range of `int`");
837836
}
838837
_numberInt = result;
839838
} else if ((_numTypesValid & NR_BIGINT) != 0) {

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -667,16 +667,15 @@ protected void reportInvalidNumber(String msg) throws JsonParseException {
667667
}
668668

669669
protected void reportOverflowInt() throws IOException {
670-
_reportError(String.format("Numeric value (%s) out of range of int (%d - %s)",
671-
_longIntegerDesc(getText()), Integer.MIN_VALUE, Integer.MAX_VALUE));
670+
_reportError("Numeric value (%s) out of range of `int` (%d - %s)",
671+
_longIntegerDesc(getText()), Integer.MIN_VALUE, Integer.MAX_VALUE);
672672
}
673673

674674
protected void reportOverflowLong() throws IOException {
675-
_reportError(String.format("Numeric value (%s) out of range of long (%d - %s)",
676-
_longIntegerDesc(getText()), Long.MIN_VALUE, Long.MAX_VALUE));
675+
_reportError("Numeric value (%s) out of range of `long` (%d - %s)",
676+
_longIntegerDesc(getText()), Long.MIN_VALUE, Long.MAX_VALUE);
677677
}
678678

679-
// @since 2.9.8
680679
protected String _longIntegerDesc(String rawNum) {
681680
int rawLen = rawNum.length();
682681
if (rawLen < 1000) {
@@ -688,7 +687,6 @@ protected String _longIntegerDesc(String rawNum) {
688687
return String.format("[Integer with %d digits]", rawLen);
689688
}
690689

691-
// @since 2.9.8
692690
protected String _longNumberDesc(String rawNum) {
693691
int rawLen = rawNum.length();
694692
if (rawLen < 1000) {
@@ -773,6 +771,10 @@ protected final void _reportError(String msg, Object arg1, Object arg2) throws J
773771
throw _constructError(String.format(msg, arg1, arg2));
774772
}
775773

774+
protected final void _reportError(String msg, Object arg1, Object arg2, Object arg3) throws JsonParseException {
775+
throw _constructError(String.format(msg, arg1, arg2, arg3));
776+
}
777+
776778
protected final void _wrapError(String msg, Throwable t) throws JsonParseException {
777779
throw _constructError(msg, t);
778780
}

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

+12-19
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ public class ReaderBasedJsonParser
2020
extends JsonParserBase
2121
{
2222
private final static int FEAT_MASK_TRAILING_COMMA = JsonReadFeature.ALLOW_TRAILING_COMMA.getMask();
23-
2423
private final static int FEAT_MASK_ALLOW_MISSING = JsonReadFeature.ALLOW_MISSING_VALUES.getMask();
25-
private final static int FEAT_MASK_ALLOW_SINGLE_QUOTES = JsonReadFeature.ALLOW_SINGLE_QUOTES.getMask();
26-
27-
private final static int FEAT_MASK_ALLOW_JAVA_COMMENTS = JsonReadFeature.ALLOW_JAVA_COMMENTS.getMask();
28-
private final static int FEAT_MASK_ALLOW_YAML_COMMENTS = JsonReadFeature.ALLOW_YAML_COMMENTS.getMask();
2924

3025
// Latin1 encoding is not supported, but we do use 8-bit subset for
3126
// pre-processing task, to simplify first pass, keep it fast.
@@ -450,9 +445,8 @@ public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
450445
} catch (IllegalArgumentException iae) {
451446
throw _constructError("Failed to decode VALUE_STRING as base64 ("+b64variant+"): "+iae.getMessage());
452447
}
453-
/* let's clear incomplete only now; allows for accessing other
454-
* textual content in error cases
455-
*/
448+
// let's clear incomplete only now; allows for accessing other
449+
// textual content in error cases
456450
_tokenIncomplete = false;
457451
} else { // may actually require conversion...
458452
if (_binaryValue == null) {
@@ -540,7 +534,7 @@ protected int _readBinary(Base64Variant b64variant, OutputStream out, byte[] buf
540534
// First branch: can get padding (-> 1 byte)
541535
if (bits < 0) {
542536
if (bits != Base64Variant.BASE64_VALUE_PADDING) {
543-
// as per [JACKSON-631], could also just be 'missing' padding
537+
// could also just be missing padding
544538
if (ch == '"' && !b64variant.usesPadding()) {
545539
decodedData >>= 4;
546540
buffer[outputPtr++] = (byte) decodedData;
@@ -575,7 +569,7 @@ protected int _readBinary(Base64Variant b64variant, OutputStream out, byte[] buf
575569
bits = b64variant.decodeBase64Char(ch);
576570
if (bits < 0) {
577571
if (bits != Base64Variant.BASE64_VALUE_PADDING) {
578-
// as per [JACKSON-631], could also just be 'missing' padding
572+
// as per could also just be missing padding
579573
if (ch == '"' && !b64variant.usesPadding()) {
580574
decodedData >>= 2;
581575
buffer[outputPtr++] = (byte) (decodedData >> 8);
@@ -1742,7 +1736,7 @@ private String _parseName2(int startPtr, int hash, int endChar) throws IOExcepti
17421736
protected String _handleOddName(int i) throws IOException
17431737
{
17441738
// Allow single quotes?
1745-
if (i == '\'' && (_formatReadFeatures & FEAT_MASK_ALLOW_SINGLE_QUOTES) != 0) {
1739+
if (i == '\'' && isEnabled(JsonReadFeature.ALLOW_SINGLE_QUOTES)) {
17461740
return _parseAposName();
17471741
}
17481742
// Allow unquoted names if feature enabled:
@@ -1755,7 +1749,7 @@ protected String _handleOddName(int i) throws IOException
17551749
// Also: first char must be a valid name char, but NOT be number
17561750
boolean firstOk;
17571751

1758-
if (i < maxCode) { // identifier, or a number ([Issue#102])
1752+
if (i < maxCode) { // identifier, or a number ([jackson-core#102])
17591753
firstOk = (codes[i] == 0);
17601754
} else {
17611755
firstOk = Character.isJavaIdentifierPart((char) i);
@@ -1836,15 +1830,14 @@ protected JsonToken _handleOddValue(int i) throws IOException
18361830
* Also, no separation to fast/slow parsing; we'll just do
18371831
* one regular (~= slowish) parsing, to keep code simple
18381832
*/
1839-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_SINGLE_QUOTES) != 0) {
1833+
if (isEnabled(JsonReadFeature.ALLOW_SINGLE_QUOTES)) {
18401834
return _handleApos();
18411835
}
18421836
break;
18431837
case ']':
1844-
/* 28-Mar-2016: [core#116]: If Feature.ALLOW_MISSING_VALUES is enabled
1845-
* we may allow "missing values", that is, encountering a trailing
1846-
* comma or closing marker where value would be expected
1847-
*/
1838+
// 28-Mar-2016: [core#116]: If Feature.ALLOW_MISSING_VALUES is enabled
1839+
// we may allow "missing values", that is, encountering a trailing
1840+
// comma or closing marker where value would be expected
18481841
if (!_parsingContext.inArray()) {
18491842
break;
18501843
}
@@ -2401,7 +2394,7 @@ private int _skipWSOrEnd2() throws IOException
24012394

24022395
private void _skipComment() throws IOException
24032396
{
2404-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_JAVA_COMMENTS) == 0) {
2397+
if (!isEnabled(JsonReadFeature.ALLOW_JAVA_COMMENTS)) {
24052398
_reportUnexpectedChar('/', "maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)");
24062399
}
24072400
// First: check which comment (if either) it is:
@@ -2451,7 +2444,7 @@ private void _skipCComment() throws IOException
24512444

24522445
private boolean _skipYAMLComment() throws IOException
24532446
{
2454-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_YAML_COMMENTS) == 0) {
2447+
if (!isEnabled(JsonReadFeature.ALLOW_YAML_COMMENTS)) {
24552448
return false;
24562449
}
24572450
_skipLine();

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public class UTF8DataInputJsonParser
3535
{
3636
final static byte BYTE_LF = (byte) '\n';
3737

38-
private final static int FEAT_MASK_TRAILING_COMMA = JsonReadFeature.ALLOW_TRAILING_COMMA.getMask();
39-
private final static int FEAT_MASK_ALLOW_JAVA_COMMENTS = JsonReadFeature.ALLOW_JAVA_COMMENTS.getMask();
40-
private final static int FEAT_MASK_ALLOW_YAML_COMMENTS = JsonReadFeature.ALLOW_YAML_COMMENTS.getMask();
41-
4238
// This is the main input-code lookup table, fetched eagerly
4339
private final static int[] _icUTF8 = CharTypes.getInputCodeUtf8();
4440

@@ -584,7 +580,7 @@ public JsonToken nextToken() throws IOException
584580
i = _skipWS();
585581

586582
// Was that a trailing comma?
587-
if ((_formatReadFeatures & FEAT_MASK_TRAILING_COMMA) != 0) {
583+
if (isEnabled(JsonReadFeature.ALLOW_TRAILING_COMMA)) {
588584
if (i == INT_RBRACKET || i == INT_RCURLY) {
589585
_closeScope(i);
590586
return _currToken;
@@ -766,7 +762,7 @@ public String nextFieldName() throws IOException
766762
i = _skipWS();
767763

768764
// Was that a trailing comma?
769-
if ((_formatReadFeatures & FEAT_MASK_TRAILING_COMMA) != 0) {
765+
if (isEnabled(JsonReadFeature.ALLOW_TRAILING_COMMA)) {
770766
if (i == INT_RBRACKET || i == INT_RCURLY) {
771767
_closeScope(i);
772768
return null;
@@ -2339,7 +2335,7 @@ private final int _skipColon2(int i, boolean gotColon) throws IOException
23392335

23402336
private final void _skipComment() throws IOException
23412337
{
2342-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_JAVA_COMMENTS) == 0) {
2338+
if (!isEnabled(JsonReadFeature.ALLOW_JAVA_COMMENTS)) {
23432339
_reportUnexpectedChar('/', "maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)");
23442340
}
23452341
int c = _inputData.readUnsignedByte();
@@ -2394,7 +2390,7 @@ private final void _skipCComment() throws IOException
23942390

23952391
private final boolean _skipYAMLComment() throws IOException
23962392
{
2397-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_YAML_COMMENTS) == 0) {
2393+
if (!isEnabled(JsonReadFeature.ALLOW_YAML_COMMENTS)) {
23982394
return false;
23992395
}
24002396
_skipLine();

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public class UTF8StreamJsonParser
2323

2424
private final static int FEAT_MASK_TRAILING_COMMA = JsonReadFeature.ALLOW_TRAILING_COMMA.getMask();
2525
private final static int FEAT_MASK_ALLOW_MISSING = JsonReadFeature.ALLOW_MISSING_VALUES.getMask();
26-
private final static int FEAT_MASK_ALLOW_JAVA_COMMENTS = JsonReadFeature.ALLOW_JAVA_COMMENTS.getMask();
27-
private final static int FEAT_MASK_ALLOW_YAML_COMMENTS = JsonReadFeature.ALLOW_YAML_COMMENTS.getMask();
2826

2927
// This is the main input-code lookup table, fetched eagerly
3028
private final static int[] _icUTF8 = CharTypes.getInputCodeUtf8();
@@ -3424,7 +3422,7 @@ private final int _skipColon2(boolean gotColon) throws IOException
34243422

34253423
private final void _skipComment() throws IOException
34263424
{
3427-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_JAVA_COMMENTS) == 0) {
3425+
if (!isEnabled(JsonReadFeature.ALLOW_JAVA_COMMENTS)) {
34283426
_reportUnexpectedChar('/', "maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)");
34293427
}
34303428
// First: check which comment (if either) it is:
@@ -3489,7 +3487,7 @@ private final void _skipCComment() throws IOException
34893487

34903488
private final boolean _skipYAMLComment() throws IOException
34913489
{
3492-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_YAML_COMMENTS) == 0) {
3490+
if (!isEnabled(JsonReadFeature.ALLOW_YAML_COMMENTS)) {
34933491
return false;
34943492
}
34953493
_skipLine();

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public class NonBlockingJsonParser
1818
{
1919
private final static int FEAT_MASK_TRAILING_COMMA = JsonReadFeature.ALLOW_TRAILING_COMMA.getMask();
2020
private final static int FEAT_MASK_ALLOW_MISSING = JsonReadFeature.ALLOW_MISSING_VALUES.getMask();
21-
private final static int FEAT_MASK_ALLOW_SINGLE_QUOTES = JsonReadFeature.ALLOW_SINGLE_QUOTES.getMask();
22-
private final static int FEAT_MASK_ALLOW_UNQUOTED_NAMES = JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES.getMask();
23-
private final static int FEAT_MASK_ALLOW_JAVA_COMMENTS = JsonReadFeature.ALLOW_JAVA_COMMENTS.getMask();
24-
private final static int FEAT_MASK_ALLOW_YAML_COMMENTS = JsonReadFeature.ALLOW_YAML_COMMENTS.getMask();
2521

2622
// This is the main input-code lookup table, fetched eagerly
2723
private final static int[] _icUTF8 = CharTypes.getInputCodeUtf8();
@@ -909,7 +905,7 @@ protected JsonToken _startUnexpectedValue(boolean leadingComma, int ch) throws I
909905
// been handled earlier
910906
break;
911907
case '\'':
912-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_SINGLE_QUOTES) != 0) {
908+
if (isEnabled(JsonReadFeature.ALLOW_SINGLE_QUOTES)) {
913909
return _startAposString();
914910
}
915911
break;
@@ -956,7 +952,7 @@ private final int _skipWS(int ch) throws IOException
956952

957953
private final JsonToken _startSlashComment(int fromMinorState) throws IOException
958954
{
959-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_JAVA_COMMENTS) == 0) {
955+
if (!isEnabled(JsonReadFeature.ALLOW_JAVA_COMMENTS)) {
960956
_reportUnexpectedChar('/', "maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)");
961957
}
962958

@@ -980,7 +976,7 @@ private final JsonToken _startSlashComment(int fromMinorState) throws IOExceptio
980976
private final JsonToken _finishHashComment(int fromMinorState) throws IOException
981977
{
982978
// Could by-pass this check by refactoring, but for now simplest way...
983-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_YAML_COMMENTS) == 0) {
979+
if (!isEnabled(JsonReadFeature.ALLOW_YAML_COMMENTS)) {
984980
_reportUnexpectedChar('#', "maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_YAML_COMMENTS' not enabled for parser)");
985981
}
986982
while (true) {
@@ -2060,25 +2056,24 @@ private JsonToken _handleOddName(int ch) throws IOException
20602056
// First: may allow single quotes
20612057
switch (ch) {
20622058
case '#':
2063-
// Careful, since this may alternatively be leading char of
2064-
// unquoted name...
2065-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_YAML_COMMENTS) != 0) {
2059+
// Careful, since this may alternatively be leading char of unquoted name...
2060+
if (isEnabled(JsonReadFeature.ALLOW_YAML_COMMENTS)) {
20662061
return _finishHashComment(MINOR_FIELD_LEADING_WS);
20672062
}
20682063
break;
20692064
case '/':
20702065
return _startSlashComment(MINOR_FIELD_LEADING_WS);
20712066
case '\'':
2072-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_SINGLE_QUOTES) != 0) {
2067+
if (isEnabled(JsonReadFeature.ALLOW_SINGLE_QUOTES)) {
20732068
return _finishAposName(0, 0, 0);
20742069
}
20752070
break;
20762071
case ']': // for better error reporting...
20772072
return _closeArrayScope();
20782073
}
20792074
// allow unquoted names if feature enabled:
2080-
if ((_formatReadFeatures & FEAT_MASK_ALLOW_UNQUOTED_NAMES) == 0) {
2081-
// !!! TODO: Decode UTF-8 characters properly...
2075+
if (!isEnabled(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES)) {
2076+
// !!! TODO: Decode UTF-8 characters properly...
20822077
// char c = (char) _decodeCharForError(ch);
20832078
char c = (char) ch;
20842079
_reportUnexpectedChar(c, "was expecting double-quote to start field name");

0 commit comments

Comments
 (0)