diff --git a/src/main/java/com/fasterxml/jackson/core/TSFBuilder.java b/src/main/java/com/fasterxml/jackson/core/TSFBuilder.java index ce8e6f847b..29c6980817 100644 --- a/src/main/java/com/fasterxml/jackson/core/TSFBuilder.java +++ b/src/main/java/com/fasterxml/jackson/core/TSFBuilder.java @@ -276,16 +276,25 @@ public B outputDecorator(OutputDecorator dec) { // // // Support for subtypes protected void _legacyEnable(JsonParser.Feature f) { - _streamReadFeatures |= f.getMask(); + if (f != null) { + _streamReadFeatures |= f.getMask(); + } } + protected void _legacyDisable(JsonParser.Feature f) { - _streamReadFeatures &= ~f.getMask(); + if (f != null) { + _streamReadFeatures &= ~f.getMask(); + } } protected void _legacyEnable(JsonGenerator.Feature f) { - _streamWriteFeatures |= f.getMask(); + if (f != null) { + _streamWriteFeatures |= f.getMask(); + } } protected void _legacyDisable(JsonGenerator.Feature f) { - _streamWriteFeatures &= ~f.getMask(); + if (f != null) { + _streamWriteFeatures &= ~f.getMask(); + } } } diff --git a/src/main/java/com/fasterxml/jackson/core/json/JsonReadFeature.java b/src/main/java/com/fasterxml/jackson/core/json/JsonReadFeature.java index f6b2871d4e..11022796b4 100644 --- a/src/main/java/com/fasterxml/jackson/core/json/JsonReadFeature.java +++ b/src/main/java/com/fasterxml/jackson/core/json/JsonReadFeature.java @@ -106,7 +106,20 @@ public enum JsonReadFeature */ @SuppressWarnings("deprecation") ALLOW_LEADING_ZEROS_FOR_NUMBERS(false, JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS), - + + /** + * Feature that determines whether parser will allow + * JSON decimal numbers to start with a decimal point + * (like: .123). If enabled, no exception is thrown, and the number + * is parsed as though a leading 0 had been present. + *

+ * Since JSON specification does not allow leading decimal, + * this is a non-standard feature, and as such disabled by default. + * + * @since 2.11 + */ + ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS(false, null), + /** * Feature that allows parser to recognize set of * "Not-a-Number" (NaN) tokens as legal floating number diff --git a/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java b/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java new file mode 100644 index 0000000000..ab95dbc06a --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/NonStandardNumbers611Test.java @@ -0,0 +1,52 @@ +package com.fasterxml.jackson.failing; + +import com.fasterxml.jackson.core.*; +import com.fasterxml.jackson.core.json.JsonReadFeature; + +public class NonStandardNumbers611Test + extends com.fasterxml.jackson.core.BaseTest +{ + /** + * The format ".NNN" (as opposed to "0.NNN") is not valid JSON, so this should fail + */ + public void testLeadingDotInDecimal() throws Exception { + for (int mode : ALL_MODES) { + JsonParser p = createParser(mode, " .123 "); + try { + p.nextToken(); + fail("Should not pass"); + } catch (JsonParseException e) { + verifyException(e, "Unexpected character ('.'"); + } + p.close(); + } + } + + public void testLeadingDotInDecimalAllowed() throws Exception { + final JsonFactory f = JsonFactory.builder() + .enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS) + .build(); + +// TODO: +/* + for (int mode : ALL_MODES) { + _testLeadingDotInDecimalAllowed(f, mode); + } + */ + + + _testLeadingDotInDecimalAllowed(f, MODE_INPUT_STREAM); + _testLeadingDotInDecimalAllowed(f, MODE_INPUT_STREAM_THROTTLED); + _testLeadingDotInDecimalAllowed(f, MODE_READER); + _testLeadingDotInDecimalAllowed(f, MODE_DATA_INPUT); + } + + private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception + { + JsonParser p = createParser(f, mode, " .123 "); + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(0.123, p.getValueAsDouble()); + assertEquals("0.123", p.getDecimalValue().toString()); + p.close(); + } +}