-
-
Notifications
You must be signed in to change notification settings - Fork 811
Add a feature to allow leading plus sign (JsonReadFeature.ALLOW_LEADING_PLUS_SIGN_FOR_NUMBERS
)
#774
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
Changes from 8 commits
2f87986
1ae2f52
0bda2ca
bb13d11
d978b86
ccb40e1
22df350
76905f5
3aec651
3dae271
b221055
5a61dfb
d0c5805
95d3a58
405a03c
2fba4df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ public class NonStandardNumberParsingTest | |
extends com.fasterxml.jackson.core.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(); | ||
|
@@ -31,6 +32,22 @@ public void testLeadingDotInDecimal() throws Exception { | |
} | ||
} | ||
|
||
/* | ||
* The format "+NNN" (as opposed to "NNN") is not valid JSON, so this should fail | ||
*/ | ||
public void testLeadingPlusSignInDecimal() throws Exception { | ||
for (int mode : ALL_MODES) { | ||
JsonParser p = createParser(mode, " +123 "); | ||
try { | ||
p.nextToken(); | ||
fail("Should not pass"); | ||
} catch (JsonParseException e) { | ||
verifyException(e, "expected digit (0-9) to follow minus sign, for valid numeric value"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cowtowncoder I've left this in draft because the existing code throws an exception with a confusing message if a plus sign precedes a number - is it ok if I use this PR to change the message to say that plus signs are not allowed in front of JSON numbers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, absolutely, improvements to error messages are good. |
||
} | ||
p.close(); | ||
} | ||
} | ||
|
||
/** | ||
* The format "NNN." (as opposed to "NNN") is not valid JSON, so this should fail | ||
*/ | ||
|
@@ -73,23 +90,58 @@ public void testTrailingDotInDecimalAllowedReader() throws Exception { | |
_testTrailingDotInDecimalAllowed(jsonFactory(), MODE_READER); | ||
} | ||
|
||
public void testLeadingPlusSignInDecimalAllowedAsync() throws Exception { | ||
_testLeadingPlusSignInDecimalAllowed(jsonFactory(), MODE_DATA_INPUT); | ||
} | ||
|
||
public void testLeadingPlusSignInDecimalAllowedBytes() throws Exception { | ||
_testLeadingPlusSignInDecimalAllowed(jsonFactory(), MODE_INPUT_STREAM); | ||
_testLeadingPlusSignInDecimalAllowed(jsonFactory(), MODE_INPUT_STREAM_THROTTLED); | ||
} | ||
|
||
public void testLeadingPlusSignInDecimalAllowedReader() throws Exception { | ||
_testLeadingPlusSignInDecimalAllowed(jsonFactory(), MODE_READER); | ||
} | ||
|
||
private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception | ||
{ | ||
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()); | ||
p.close(); | ||
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()); | ||
} | ||
} | ||
|
||
private void _testLeadingPlusSignInDecimalAllowed(JsonFactory f, int mode) throws Exception | ||
{ | ||
try (JsonParser p = createParser(f, mode, " +125 ")) { | ||
assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken()); | ||
assertEquals(125.0, p.getValueAsDouble()); | ||
assertEquals("125", p.getDecimalValue().toString()); | ||
assertEquals("125", p.getText()); | ||
} | ||
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()); | ||
} | ||
} | ||
|
||
private void _testTrailingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception | ||
{ | ||
JsonParser p = createParser(f, mode, " 125. "); | ||
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); | ||
assertEquals(125.0, p.getValueAsDouble()); | ||
assertEquals("125", p.getDecimalValue().toString()); | ||
assertEquals("125.", p.getText()); | ||
p.close(); | ||
try (JsonParser p = createParser(f, mode, " 125. ")) { | ||
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); | ||
assertEquals(125.0, p.getValueAsDouble()); | ||
assertEquals("125", p.getDecimalValue().toString()); | ||
assertEquals("125.", p.getText()); | ||
} | ||
try (JsonParser p = createParser(f, mode, " 125. ")) { | ||
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); | ||
assertEquals(125.0, p.getValueAsDouble()); | ||
assertEquals("125", p.getDecimalValue().toString()); | ||
assertEquals("125.", p.getText()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this is problematic as it allows something like
++++4
I think. Or, potentially, DoS with 10,000 plus signs. :)But I think it should be possible to handle this as a new entry in
switch
below, maybe adding case right above0
...9
, reading next character, falling through?Same goes for all parsers.