Skip to content

Commit

Permalink
Support large numbers
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Smithson <smithson@uk.ibm.com>
  • Loading branch information
crshnburn committed Mar 8, 2021
1 parent fa4aa8e commit e9f770e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/ibm/json/java/internal/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Tokenizes a stream into JSON tokens.
Expand Down Expand Up @@ -301,7 +303,7 @@ private Number readNumber() throws IOException {

try {
if (-1 != string.indexOf('.')) {
return Double.valueOf(string);
return new BigDecimal(string);
}

String sign = "";
Expand All @@ -311,26 +313,26 @@ private Number readNumber() throws IOException {
}

if (string.toUpperCase().startsWith("0X")) {
return Long.valueOf(sign + string.substring(2), 16);
return new BigInteger(sign + string.substring(2), 16);
}

if (string.equals("0")) {
return Long.valueOf(0);
return BigInteger.ZERO;
}

/**
* We have to check for the exponential and treat appropriately
* Exponentials should be treated as Doubles.
*/
if (string.indexOf("e") != -1 || string.indexOf("E") != -1) {
return Double.valueOf(sign + string);
return new BigDecimal(sign + string);
}

if (string.startsWith("0") && string.length() > 1) {
return Long.valueOf(sign + string.substring(1), 8);
return new BigInteger(sign + string.substring(1), 8);
}

return Long.valueOf(sign + string, 10);
return new BigInteger(sign + string, 10);
} catch (NumberFormatException e) {
IOException iox = new IOException("Invalid number literal " + onLineCol(l, c));
iox.initCause(e);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/ibm/json4j/JSONNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class JSONNumberTest {
* Test that a whole number larger than a 64bit whole number can be parsed.
* @throws IOException If there is an error parsing the JSON
*/
//@Test
@Test
public void testLargeWholeNumber() throws IOException {
String inputJson = "{\"number\":18446744073709551615}";

Expand Down Expand Up @@ -64,7 +64,7 @@ public void testDecimalNumber() throws IOException {
* Test that a decimal number larger than a Java double can be parsed
* @throws IOException If there is an error parsing the JSON
*/
//@Test
@Test
public void testLargeDecimalNumber() throws IOException {
String inputJson = "{\"number\":2.0e500}";

Expand Down

0 comments on commit e9f770e

Please sign in to comment.