Skip to content

Commit df4e7bd

Browse files
authored
add async parser tests for leading dots (and make changes to get them to pass) (#799)
1 parent 47734d6 commit df4e7bd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,17 @@ protected JsonToken _finishNumberPlusMinus(final int ch, final boolean negative)
15741574
}
15751575
return _finishNumberLeadingPosZeroes();
15761576
}
1577+
} else if (ch == INT_PERIOD && isEnabled(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS.mappedFeature())) {
1578+
if (negative) {
1579+
_inputPtr--;
1580+
return _finishNumberLeadingNegZeroes();
1581+
} else {
1582+
if (!isEnabled(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS.mappedFeature())) {
1583+
_reportUnexpectedNumberChar('+', "JSON spec does not allow numbers to have plus signs: enable `JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS` to allow");
1584+
}
1585+
_inputPtr--;
1586+
return _finishNumberLeadingPosZeroes();
1587+
}
15771588
}
15781589
final String message = negative ?
15791590
"expected digit (0-9) to follow minus sign, for valid numeric value" :

src/test/java/com/fasterxml/jackson/core/json/async/AsyncNonStandardNumberParsingTest.java

+70
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,76 @@ public void testLeadingPlusSignInDecimalEnabled2() throws Exception {
220220
}
221221
}
222222

223+
public void testLeadingPlusSignInDecimalEnabled3() throws Exception {
224+
final String JSON = "[ +123.123 ]";
225+
226+
JsonFactory jsonFactory =
227+
JsonFactory.builder().enable(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS).build();
228+
AsyncReaderWrapper p = createParser(jsonFactory, JSON, 1);
229+
assertToken(JsonToken.START_ARRAY, p.nextToken());
230+
try {
231+
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
232+
assertEquals(123.123, p.getDoubleValue());
233+
assertEquals("123.123", p.getDecimalValue().toString());
234+
assertEquals("+123.123", p.currentText());
235+
} finally {
236+
p.close();
237+
}
238+
}
239+
240+
public void testLeadingPlusSignNoLeadingZeroDisabled() throws Exception {
241+
final String JSON = "[ +.123 ]";
242+
243+
JsonFactory jsonFactory = JsonFactory.builder()
244+
.enable(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS)
245+
.build();
246+
AsyncReaderWrapper p = createParser(jsonFactory, JSON, 1);
247+
assertToken(JsonToken.START_ARRAY, p.nextToken());
248+
try {
249+
p.nextToken();
250+
fail("Expected exception");
251+
} catch (StreamReadException e) {
252+
verifyException(e, "Unexpected character ('.'");
253+
}
254+
}
255+
256+
public void testLeadingPlusSignNoLeadingZeroEnabled() throws Exception {
257+
final String JSON = "[ +.123 ]";
258+
259+
JsonFactory jsonFactory = JsonFactory.builder()
260+
.enable(JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS)
261+
.enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS)
262+
.build();
263+
AsyncReaderWrapper p = createParser(jsonFactory, JSON, 1);
264+
assertToken(JsonToken.START_ARRAY, p.nextToken());
265+
try {
266+
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
267+
assertEquals(0.123, p.getDoubleValue());
268+
assertEquals("0.123", p.getDecimalValue().toString());
269+
assertEquals("+0.123", p.currentText());
270+
} finally {
271+
p.close();
272+
}
273+
}
274+
275+
public void testLeadingDotInNegativeDecimalEnabled() throws Exception {
276+
final String JSON = "[ -.123 ]";
277+
final JsonFactory factory = JsonFactory.builder()
278+
.enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS)
279+
.build();
280+
281+
AsyncReaderWrapper p = createParser(factory, JSON, 1);
282+
assertToken(JsonToken.START_ARRAY, p.nextToken());
283+
try {
284+
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
285+
assertEquals(-0.123, p.getDoubleValue());
286+
assertEquals("-0.123", p.getDecimalValue().toString());
287+
assertEquals("-0.123", p.currentText());
288+
} finally {
289+
p.close();
290+
}
291+
}
292+
223293
/**
224294
* The format "NNN." (as opposed to "NNN") is not valid JSON, so this should fail
225295
*/

0 commit comments

Comments
 (0)