Skip to content

Commit

Permalink
Merge pull request #1 from zosconnect/numberhandling
Browse files Browse the repository at this point in the history
Support large numbers
  • Loading branch information
crshnburn authored Mar 22, 2021
2 parents 3f6ecb1 + 974e6c9 commit c740f35
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ java {
}

jar {
project.version '1.0.0'
project.version '2.0.0'
}

dependencies {
Expand All @@ -34,7 +34,7 @@ publishing {
maven(MavenPublication) {
groupId = 'com.ibm.zosconnect'
artifactId = 'json4j'
version = '1.0.0'
version = '2.0.0'

from components.java
}
Expand Down
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
2 changes: 1 addition & 1 deletion src/main/java/com/ibm/json/java/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
/**
* @version 1.0.0
*/
@org.osgi.annotation.versioning.Version("1.0.0")
@org.osgi.annotation.versioning.Version("2.0.0")
package com.ibm.json.java;
4 changes: 2 additions & 2 deletions src/main/java/com/ibm/json/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* IBM Corporation - initial API and implementation
*******************************************************************************/
/**
* @version 1.0.0
* @version 2.0.0
*/
@org.osgi.annotation.versioning.Version("1.0.0")
@org.osgi.annotation.versioning.Version("2.0.0")
package com.ibm.json;
2 changes: 1 addition & 1 deletion src/main/java/com/ibm/json/xml/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
/**
* @version 1.0.0
*/
@org.osgi.annotation.versioning.Version("1.0.0")
@org.osgi.annotation.versioning.Version("2.0.0")
package com.ibm.json.xml;
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 c740f35

Please sign in to comment.