Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with parsing '-.125' when leading decimal points allowed #777

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1327,12 +1327,17 @@ public final Boolean nextBooleanValue() throws IOException

// @since 2.11, [core#611]
protected final JsonToken _parseFloatThatStartsWithPeriod() throws IOException
{
return _parseFloatThatStartsWithPeriod(false);
}

protected final JsonToken _parseFloatThatStartsWithPeriod(final boolean neg) throws IOException
{
// [core#611]: allow optionally leading decimal point
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS.mappedFeature())) {
return _handleOddValue('.');
}
return _parseFloat(INT_PERIOD, _inputPtr-1, _inputPtr, false, 0);
return _parseFloat(INT_PERIOD, _inputPtr-1, _inputPtr, neg, 0);
}

/**
Expand Down Expand Up @@ -1491,7 +1496,7 @@ private final JsonToken _parseSignedNumber(final boolean negative) throws IOExce
if (ch > INT_9 || ch < INT_0) {
_inputPtr = ptr;
if (ch == INT_PERIOD) {
return _parseFloatThatStartsWithPeriod();
return _parseFloatThatStartsWithPeriod(negative);
}
return _handleInvalidNumberStart(ch, negative, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,18 @@ public Boolean nextBooleanValue() throws IOException

// @since 2.11, [core#611]
protected final JsonToken _parseFloatThatStartsWithPeriod() throws IOException
{
return _parseFloatThatStartsWithPeriod(false);
}

protected final JsonToken _parseFloatThatStartsWithPeriod(final boolean neg) throws IOException
{
// [core#611]: allow optionally leading decimal point
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS.mappedFeature())) {
return _handleUnexpectedValue(INT_PERIOD);
}
char[] outBuf = _textBuffer.emptyAndGetCurrentSegment();
return _parseFloat(outBuf, 0, INT_PERIOD, false, 0);
return _parseFloat(outBuf, 0, INT_PERIOD, neg, 0);
}

/**
Expand Down Expand Up @@ -1107,7 +1112,7 @@ private final JsonToken _parseSignedNumber(boolean negative) throws IOException
if (c == INT_0) {
c = _handleLeadingZeroes();
} else if (c == INT_PERIOD) {
return _parseFloatThatStartsWithPeriod();
return _parseFloatThatStartsWithPeriod(negative);
} else {
return _handleInvalidNumberStart(c, negative, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1432,13 +1432,18 @@ public Boolean nextBooleanValue() throws IOException

// @since 2.11, [core#611]
protected final JsonToken _parseFloatThatStartsWithPeriod() throws IOException
{
return _parseFloatThatStartsWithPeriod(false);
}

protected final JsonToken _parseFloatThatStartsWithPeriod(final boolean neg) throws IOException
{
// [core#611]: allow optionally leading decimal point
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS.mappedFeature())) {
return _handleUnexpectedValue(INT_PERIOD);
}
return _parseFloat(_textBuffer.emptyAndGetCurrentSegment(),
0, INT_PERIOD, false, 0);
0, INT_PERIOD, neg, 0);
}

/**
Expand Down Expand Up @@ -1522,7 +1527,7 @@ private final JsonToken _parseSignedNumber(boolean negative) throws IOException
// One special case: if first char is 0, must not be followed by a digit
if (c != INT_0) {
if (c == INT_PERIOD) {
return _parseFloatThatStartsWithPeriod();
return _parseFloatThatStartsWithPeriod(negative);
}
return _handleInvalidNumberStart(c, negative, true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.fasterxml.jackson.failing;

import com.fasterxml.jackson.core.BaseTest;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.json.JsonReadFeature;

public class FailingNonStandardNumberParsingTest
extends BaseTest
{
private final JsonFactory JSON_F = JsonFactory.builder()
.enable(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS)
.enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS)
.enable(JsonReadFeature.ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS)
.build();

protected JsonFactory jsonFactory() {
return JSON_F;
}

public void testLeadingDotInNegativeDecimalAllowedAsync() throws Exception {
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_DATA_INPUT);
}

public void testLeadingDotInNegativeDecimalAllowedBytes() throws Exception {
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_INPUT_STREAM);
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_INPUT_STREAM_THROTTLED);
}

public void testLeadingDotInNegativeDecimalAllowedReader() throws Exception {
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER);
}

private void _testLeadingDotInNegativeDecimalAllowed(JsonFactory f, int mode) throws Exception
{
try (JsonParser p = createParser(f, mode, " -.125 ")) {
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertEquals(-0.125, p.getValueAsDouble());
assertEquals("-0.125", p.getDecimalValue().toString());
assertEquals("-.125", p.getText());
}
}
}