Skip to content
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

optLong vs getLong inconsistencies #783

Merged
merged 4 commits into from Oct 13, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 60 additions & 11 deletions src/main/java/org/json/JSONObject.java
Expand Up @@ -2385,14 +2385,21 @@ protected static boolean isDecimalNotation(final String val) {
* returns for this function are BigDecimal, Double, BigInteger, Long, and Integer.
* When a Double is returned, it should always be a valid Double and not NaN or +-infinity.
*
* @param val value to convert
* @param input value to convert
* @return Number representation of the value.
* @throws NumberFormatException thrown if the value is not a valid number. A public
* caller should catch this and wrap it in a {@link JSONException} if applicable.
*/
protected static Number stringToNumber(final String val) throws NumberFormatException {
protected static Number stringToNumber(final String input) throws NumberFormatException {
String val = input;
if (val.startsWith(".")){
val = "0"+val;
}
if (val.startsWith("-.")){
val = "-0."+val.substring(2);
}
char initial = val.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
if ((initial >= '0' && initial <= '9') || initial == '-' ) {
// decimal representation
if (isDecimalNotation(val)) {
// Use a BigDecimal all the time so we keep the original
rudrajyotib marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -2409,25 +2416,26 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
try {
Double d = Double.valueOf(val);
if(d.isNaN() || d.isInfinite()) {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}
return d;
} catch (NumberFormatException ignore) {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}
}
}
// block items like 00 01 etc. Java number parsers treat these as Octal.
val = removeLeadingZerosOfNumber(input);
initial = val.charAt(0);
if(initial == '0' && val.length() > 1) {
char at1 = val.charAt(1);
if(at1 >= '0' && at1 <= '9') {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}
} else if (initial == '-' && val.length() > 2) {
char at1 = val.charAt(1);
char at2 = val.charAt(2);
if(at1 == '0' && at2 >= '0' && at2 <= '9') {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}
}
// integer representation.
Expand All @@ -2447,7 +2455,7 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
}
return bi;
}
throw new NumberFormatException("val ["+val+"] is not a valid number.");
throw new NumberFormatException("val ["+input+"] is not a valid number.");
}

/**
Expand Down Expand Up @@ -2483,8 +2491,7 @@ public static Object stringToValue(String string) {
* produced, then the value will just be a string.
*/

char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
if (potentialNumber(string)) {
try {
return stringToNumber(string);
} catch (Exception ignore) {
Expand All @@ -2493,6 +2500,28 @@ public static Object stringToValue(String string) {
return string;
}


private static boolean potentialNumber(String value){
if (value == null || value.isEmpty()){
return false;
}
return potentialPositiveNumberStartingAtIndex(value, (value.charAt(0)=='-'?1:0));
}

private static boolean potentialPositiveNumberStartingAtIndex(String value,int index){
if (index >= value.length()){
return false;
}
return digitAtIndex(value, (value.charAt(index)=='.'?index+1:index));
}

private static boolean digitAtIndex(String value, int index){
if (index >= value.length()){
return false;
}
return value.charAt(index) >= '0' && value.charAt(index) <= '9';
}

/**
* Throw an exception if the object is a NaN or infinite number.
*
Expand Down Expand Up @@ -2892,4 +2921,24 @@ private static JSONException recursivelyDefinedObjectException(String key) {
"JavaBean object contains recursively defined member variable of key " + quote(key)
);
}

/**
* For a prospective number, remove the leading zeros
* @param value prospective number
* @return number without leading zeros
*/
private static String removeLeadingZerosOfNumber(String value){
if (value.equals("-")){return value;}
boolean negativeFirstChar = (value.charAt(0) == '-');
int counter = negativeFirstChar ? 1:0;
while (counter < value.length()){
if (value.charAt(counter) != '0'){
if (negativeFirstChar) {return "-".concat(value.substring(counter));}
return value.substring(counter);
}
++counter;
}
if (negativeFirstChar) {return "-0";}
return "0";
}
}
100 changes: 100 additions & 0 deletions src/test/java/org/json/junit/JSONObjectDecimalTest.java
@@ -0,0 +1,100 @@
package org.json.junit;

import org.json.JSONObject;
import org.junit.Test;

import java.math.BigDecimal;
import java.math.BigInteger;

import static org.junit.Assert.assertEquals;

public class JSONObjectDecimalTest {

@Test
public void shouldParseDecimalNumberThatStartsWithDecimalPoint(){
JSONObject jsonObject = new JSONObject("{value:0.50}");
assertEquals("Float not recognized", 0.5f, jsonObject.getFloat("value"), 0.0f);
assertEquals("Float not recognized", 0.5f, jsonObject.optFloat("value"), 0.0f);
assertEquals("Float not recognized", 0.5f, jsonObject.optFloatObject("value"), 0.0f);
assertEquals("Double not recognized", 0.5d, jsonObject.optDouble("value"), 0.0f);
assertEquals("Double not recognized", 0.5d, jsonObject.optDoubleObject("value"), 0.0f);
assertEquals("Double not recognized", 0.5d, jsonObject.getDouble("value"), 0.0f);
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.5).compareTo(jsonObject.getBigDecimal("value")));
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
}



@Test
public void shouldParseNegativeDecimalNumberThatStartsWithDecimalPoint(){
JSONObject jsonObject = new JSONObject("{value:-.50}");
assertEquals("Float not recognized", -0.5f, jsonObject.getFloat("value"), 0.0f);
assertEquals("Float not recognized", -0.5f, jsonObject.optFloat("value"), 0.0f);
assertEquals("Float not recognized", -0.5f, jsonObject.optFloatObject("value"), 0.0f);
assertEquals("Double not recognized", -0.5d, jsonObject.optDouble("value"), 0.0f);
assertEquals("Double not recognized", -0.5d, jsonObject.optDoubleObject("value"), 0.0f);
assertEquals("Double not recognized", -0.5d, jsonObject.getDouble("value"), 0.0f);
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.5).compareTo(jsonObject.getBigDecimal("value")));
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
}

@Test
public void shouldParseDecimalNumberThatHasZeroBeforeWithDecimalPoint(){
JSONObject jsonObject = new JSONObject("{value:00.050}");
assertEquals("Float not recognized", 0.05f, jsonObject.getFloat("value"), 0.0f);
assertEquals("Float not recognized", 0.05f, jsonObject.optFloat("value"), 0.0f);
assertEquals("Float not recognized", 0.05f, jsonObject.optFloatObject("value"), 0.0f);
assertEquals("Double not recognized", 0.05d, jsonObject.optDouble("value"), 0.0f);
assertEquals("Double not recognized", 0.05d, jsonObject.optDoubleObject("value"), 0.0f);
assertEquals("Double not recognized", 0.05d, jsonObject.getDouble("value"), 0.0f);
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(.05).compareTo(jsonObject.getBigDecimal("value")));
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
}

@Test
public void shouldParseNegativeDecimalNumberThatHasZeroBeforeWithDecimalPoint(){
JSONObject jsonObject = new JSONObject("{value:-00.050}");
assertEquals("Float not recognized", -0.05f, jsonObject.getFloat("value"), 0.0f);
assertEquals("Float not recognized", -0.05f, jsonObject.optFloat("value"), 0.0f);
assertEquals("Float not recognized", -0.05f, jsonObject.optFloatObject("value"), 0.0f);
assertEquals("Double not recognized", -0.05d, jsonObject.optDouble("value"), 0.0f);
assertEquals("Double not recognized", -0.05d, jsonObject.optDoubleObject("value"), 0.0f);
assertEquals("Double not recognized", -0.05d, jsonObject.getDouble("value"), 0.0f);
assertEquals("Long not recognized", 0, jsonObject.optLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.getLong("value"), 0);
assertEquals("Long not recognized", 0, jsonObject.optLongObject("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.getInt("value"), 0);
assertEquals("Integer not recognized", 0, jsonObject.optIntegerObject("value"), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").intValue(), 0);
assertEquals("Number not recognized", 0, jsonObject.getNumber("value").longValue(), 0);
assertEquals("BigDecimal not recognized", 0, BigDecimal.valueOf(-.05).compareTo(jsonObject.getBigDecimal("value")));
assertEquals("BigInteger not recognized",0, BigInteger.valueOf(0).compareTo(jsonObject.getBigInteger("value")));
}


}
6 changes: 5 additions & 1 deletion src/test/java/org/json/junit/JSONObjectNumberTest.java
Expand Up @@ -23,7 +23,10 @@ public class JSONObjectNumberTest {
@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"{value:50}", 1},
{"{value:0050}", 1},
{"{value:0050.0000}", 1},
{"{value:-0050}", -1},
{"{value:-0050.0000}", -1},
rudrajyotib marked this conversation as resolved.
Show resolved Hide resolved
{"{value:50.0}", 1},
{"{value:5e1}", 1},
{"{value:5E1}", 1},
Expand All @@ -32,6 +35,7 @@ public static Collection<Object[]> data() {
{"{value:-50}", -1},
{"{value:-50.0}", -1},
{"{value:-5e1}", -1},
{"{value:-0005e1}", -1},
{"{value:-5E1}", -1},
{"{value:-5e1}", -1},
{"{value:'-50'}", -1}
Expand Down