Skip to content

Commit 4cec80a

Browse files
committed
Work on #611, manually merge data-input version first
1 parent ebdc0b5 commit 4cec80a

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,13 @@ public enum Feature {
170170
*/
171171
@Deprecated
172172
ALLOW_NUMERIC_LEADING_ZEROS(false),
173-
173+
174+
/**
175+
* @deprecated Use {@link com.fasterxml.jackson.core.json.JsonReadFeature#ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS} instead
176+
*/
177+
@Deprecated
178+
ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS(false),
179+
174180
/**
175181
* Feature that allows parser to recognize set of
176182
* "Not-a-Number" (NaN) tokens as legal floating number

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public enum JsonReadFeature
118118
*
119119
* @since 2.11
120120
*/
121-
ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS(false, null),
121+
@SuppressWarnings("deprecation")
122+
ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS(false, JsonParser.Feature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS),
122123

123124
/**
124125
* Feature that allows parser to recognize set of

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ public JsonToken nextToken() throws IOException
670670
case '7':
671671
case '8':
672672
case '9':
673+
case '.': // as per [core#611]
673674
t = _parsePosNumber(i);
674675
break;
675676
case 'f':
@@ -735,6 +736,7 @@ private final JsonToken _nextTokenNotInObject(int i) throws IOException
735736
case '7':
736737
case '8':
737738
case '9':
739+
case '.': // as per [core#611]
738740
return (_currToken = _parsePosNumber(i));
739741
}
740742
return (_currToken = _handleUnexpectedValue(i));
@@ -840,6 +842,7 @@ public String nextFieldName() throws IOException
840842
case '7':
841843
case '8':
842844
case '9':
845+
case '.': // as per [core#611]
843846
t = _parsePosNumber(i);
844847
break;
845848
case 'f':
@@ -996,7 +999,8 @@ protected JsonToken _parsePosNumber(int c) throws IOException
996999
{
9971000
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment();
9981001
int outPtr;
999-
1002+
final boolean forceFloat;
1003+
10001004
// One special case: if first char is 0, must not be followed by a digit.
10011005
// Gets bit tricky as we only want to retain 0 if it's the full value
10021006
if (c == INT_0) {
@@ -1007,7 +1011,16 @@ protected JsonToken _parsePosNumber(int c) throws IOException
10071011
outBuf[0] = '0';
10081012
outPtr = 1;
10091013
}
1014+
forceFloat = false;
10101015
} else {
1016+
forceFloat = (c == INT_PERIOD);
1017+
if (forceFloat) {
1018+
// [core#611]: allow optionally leading decimal point
1019+
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS.mappedFeature())) {
1020+
return _handleUnexpectedValue(c);
1021+
}
1022+
}
1023+
10111024
outBuf[0] = (char) c;
10121025
c = _inputData.readUnsignedByte();
10131026
outPtr = 1;
@@ -1024,7 +1037,7 @@ protected JsonToken _parsePosNumber(int c) throws IOException
10241037
outBuf[outPtr++] = (char) c;
10251038
c = _inputData.readUnsignedByte();
10261039
}
1027-
if (c == '.' || c == 'e' || c == 'E') {
1040+
if (c == '.' || c == 'e' || c == 'E' || forceFloat) {
10281041
return _parseFloat(outBuf, outPtr, c, false, intLen);
10291042
}
10301043
_textBuffer.setCurrentLength(outPtr);

src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,30 @@ public void testLeadingDotInDecimal() throws Exception {
2222
}
2323
}
2424

25-
public void testLeadingDotInDecimalAllowed() throws Exception {
26-
final JsonFactory f = JsonFactory.builder()
27-
.enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS)
28-
.build();
29-
30-
// TODO:
31-
/*
32-
for (int mode : ALL_MODES) {
33-
_testLeadingDotInDecimalAllowed(f, mode);
34-
}
35-
*/
25+
private final JsonFactory JSON_F = JsonFactory.builder()
26+
.enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS)
27+
.build();
28+
29+
public void testLeadingDotInDecimalAllowedAsync() throws Exception {
30+
_testLeadingDotInDecimalAllowed(JSON_F, MODE_DATA_INPUT);
31+
}
3632

33+
public void testLeadingDotInDecimalAllowedBytes() throws Exception {
34+
_testLeadingDotInDecimalAllowed(JSON_F, MODE_INPUT_STREAM);
35+
_testLeadingDotInDecimalAllowed(JSON_F, MODE_INPUT_STREAM_THROTTLED);
36+
}
3737

38-
_testLeadingDotInDecimalAllowed(f, MODE_INPUT_STREAM);
39-
_testLeadingDotInDecimalAllowed(f, MODE_INPUT_STREAM_THROTTLED);
40-
_testLeadingDotInDecimalAllowed(f, MODE_READER);
41-
_testLeadingDotInDecimalAllowed(f, MODE_DATA_INPUT);
38+
public void testLeadingDotInDecimalAllowedReader() throws Exception {
39+
_testLeadingDotInDecimalAllowed(JSON_F, MODE_READER);
4240
}
4341

4442
private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception
4543
{
46-
JsonParser p = createParser(f, mode, " .123 ");
44+
JsonParser p = createParser(f, mode, " .125 ");
4745
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
48-
assertEquals(0.123, p.getValueAsDouble());
49-
assertEquals("0.123", p.getDecimalValue().toString());
46+
assertEquals(0.125, p.getValueAsDouble());
47+
assertEquals("0.125", p.getDecimalValue().toString());
48+
assertEquals(".125", p.getText());
5049
p.close();
5150
}
5251
}

0 commit comments

Comments
 (0)