diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index a1f4986f69..66509e3785 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -231,3 +231,7 @@ Michel Feinstein (feinstein@github) Oleksandr Poslavskyi (alevskyi@github) * Contributed implementation of #504: Add a String Array write method in the Streaming API (2.11.0) + +James Agnew (jamesagnew@github) + * Contributed implementation of #611: Optionally allow leading decimal in float tokens + (2.11.0) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index ccd4b16044..4ad508b587 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -25,6 +25,8 @@ JSON library. #606: Do not clear aggregated contents of `TextBuffer` when `releaseBuffers()` called #609: `FilteringGeneratorDelegate` does not handle `writeString(Reader, int)` (reported by Volkan Y) +#611: Optionally allow leading decimal in float tokens + (contributed by James A) 2.10.4 (not yet released) diff --git a/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java index 3f423f6016..97004cece3 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/UTF8DataInputJsonParser.java @@ -983,7 +983,6 @@ public Boolean nextBooleanValue() throws IOException /********************************************************** */ - // @since 2.11, [core#611] protected final JsonToken _parseFloatThatStartsWithPeriod() throws IOException { @@ -992,9 +991,7 @@ protected final JsonToken _parseFloatThatStartsWithPeriod() throws IOException return _handleUnexpectedValue(INT_PERIOD); } char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); - outBuf[0] = '.'; - int c = _inputData.readUnsignedByte(); - return _parseFloat(outBuf, 1, c, false, 0); + return _parseFloat(outBuf, 0, INT_PERIOD, false, 0); } /** diff --git a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingJsonParser.java b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingJsonParser.java index 3649674b30..297728fda4 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingJsonParser.java +++ b/src/main/java/com/fasterxml/jackson/core/json/async/NonBlockingJsonParser.java @@ -8,6 +8,7 @@ import com.fasterxml.jackson.core.async.NonBlockingInputFeeder; import com.fasterxml.jackson.core.io.CharTypes; import com.fasterxml.jackson.core.io.IOContext; +import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; import com.fasterxml.jackson.core.util.VersionUtil; @@ -625,6 +626,13 @@ private final JsonToken _startValue(int ch) throws IOException // Should we have separate handling for plus? Although // it is not allowed per se, it may be erroneously used, // and could be indicate by a more specific error message. + + case '.': // [core#611] + if (isEnabled(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS.mappedFeature())) { + return _startFloatThatStartsWithPeriod(); + } + break; + case '0': return _startNumberLeadingZero(); case '1': @@ -1286,6 +1294,15 @@ protected JsonToken _reportErrorToken(String actualToken) throws IOException /********************************************************************** */ + // [core#611]: allow non-standard floats like ".125" + protected JsonToken _startFloatThatStartsWithPeriod() throws IOException + { + _numberNegative = false; + _intLength = 0; + char[] outBuf = _textBuffer.emptyAndGetCurrentSegment(); + return _startFloat(outBuf, 0, INT_PERIOD); + } + protected JsonToken _startPositiveNumber(int ch) throws IOException { _numberNegative = false; diff --git a/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java b/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java index c6109b72a5..b7736f97f5 100644 --- a/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java +++ b/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java @@ -6,6 +6,10 @@ public class NonStandardNumbers611Test extends com.fasterxml.jackson.core.BaseTest { + private final JsonFactory JSON_F = JsonFactory.builder() + .enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS) + .build(); + /** * The format ".NNN" (as opposed to "0.NNN") is not valid JSON, so this should fail */ @@ -22,10 +26,6 @@ public void testLeadingDotInDecimal() throws Exception { } } - private final JsonFactory JSON_F = JsonFactory.builder() - .enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS) - .build(); - public void testLeadingDotInDecimalAllowedAsync() throws Exception { _testLeadingDotInDecimalAllowed(JSON_F, MODE_DATA_INPUT); }