Skip to content

Fix #146: Underscores in numbers #203

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

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 @@ -743,12 +743,12 @@ protected void _parseNumericValue(int expType) throws IOException
{
// Int or float?
if (_currToken == JsonToken.VALUE_NUMBER_INT) {
int len = _textValue.length();
int len = _cleanedTextValue.length();
if (_numberNegative) {
len--;
}
if (len <= 9) { // definitely fits in int
_numberInt = Integer.parseInt(_textValue);
_numberInt = Integer.parseInt(_cleanedTextValue);
_numTypesValid = NR_INT;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.ModuleTestBase;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
Expand Down Expand Up @@ -299,6 +300,15 @@ public void testIntParsingUnderscoresSm() throws Exception
p.close();
}

public void testYamlLongWithUnderscores() throws Exception
{
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
LongHolder longHolder = mapper.readValue("v: 1_000_000", LongHolder.class);
assertNotNull(longHolder);
assertEquals(LongHolder.class, longHolder.getClass());
assertEquals(Long.valueOf(1000000), longHolder.getV());
}

// [cbor#4]: accidental recognition as double, with multiple dots
public void testDoubleParsing() throws Exception
{
Expand Down Expand Up @@ -571,4 +581,19 @@ public void testTimeLikeValues() throws Exception
assertNull(p.nextToken());
p.close();
}

static class LongHolder
{
private Long v;

public Long getV()
{
return v;
}

public void setV(Long v)
{
this.v = v;
}
}
}