1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

changes number parsing to use BigDecimal as the backing type

* updated tests to support BigDecimal as the backing type for numbers
* updated some test resource handling to java7 try-with-resources format
* cleaned up some other minor compiler warnings
This commit is contained in:
John J. Aylward 2018-12-10 13:19:31 -05:00
parent 956bdfa5b7
commit 56d33b8061
13 changed files with 248 additions and 255 deletions

View file

@ -2109,48 +2109,54 @@ public class JSONObject {
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
// decimal representation // decimal representation
if (isDecimalNotation(val)) { if (isDecimalNotation(val)) {
// quick dirty way to see if we need a BigDecimal instead of a Double // Use a BigDecimal all the time so we keep the original
// this only handles some cases of overflow or underflow // representation. BigDecimal doesn't support -0.0, ensure we
if (val.length()>14) { // keep that by forcing a decimal.
return new BigDecimal(val); try {
BigDecimal bd = new BigDecimal(val);
if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
return Double.valueOf(-0.0);
}
return bd;
} catch (NumberFormatException retryAsDouble) {
// this is to support "Hex Floats" like this: 0x1.0P-1074
try {
Double d = Double.valueOf(val);
if(d.isNaN() || d.isInfinite()) {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
return d;
} catch (NumberFormatException ignore) {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
} }
final Double d = Double.valueOf(val); }
if (d.isInfinite() || d.isNaN()) { // block items like 00 01 etc. Java number parsers treat these as Octal.
// if we can't parse it as a double, go up to BigDecimal if(initial == '0' && val.length() > 1) {
// this is probably due to underflow like 4.32e-678 char at1 = val.charAt(1);
// or overflow like 4.65e5324. The size of the string is small if(at1 >= '0' && at1 <= '9') {
// but can't be held in a Double. throw new NumberFormatException("val ["+val+"] is not a valid number.");
return new BigDecimal(val); }
} 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.");
} }
return d;
} }
// integer representation. // integer representation.
// This will narrow any values to the smallest reasonable Object representation // This will narrow any values to the smallest reasonable Object representation
// (Integer, Long, or BigInteger) // (Integer, Long, or BigInteger)
// string version // BigInteger down conversion: We use a similar bitLenth compare as
// The compare string length method reduces GC,
// but leads to smaller integers being placed in larger wrappers even though not
// needed. i.e. 1,000,000,000 -> Long even though it's an Integer
// 1,000,000,000,000,000,000 -> BigInteger even though it's a Long
//if(val.length()<=9){
// return Integer.valueOf(val);
//}
//if(val.length()<=18){
// return Long.valueOf(val);
//}
//return new BigInteger(val);
// BigInteger version: We use a similar bitLength compare as
// BigInteger#intValueExact uses. Increases GC, but objects hold // BigInteger#intValueExact uses. Increases GC, but objects hold
// only what they need. i.e. Less runtime overhead if the value is // only what they need. i.e. Less runtime overhead if the value is
// long lived. Which is the better tradeoff? This is closer to what's // long lived.
// in stringToValue.
BigInteger bi = new BigInteger(val); BigInteger bi = new BigInteger(val);
if(bi.bitLength()<=31){ if(bi.bitLength() <= 31){
return Integer.valueOf(bi.intValue()); return Integer.valueOf(bi.intValue());
} }
if(bi.bitLength()<=63){ if(bi.bitLength() <= 63){
return Long.valueOf(bi.longValue()); return Long.valueOf(bi.longValue());
} }
return bi; return bi;
@ -2194,23 +2200,7 @@ public class JSONObject {
char initial = string.charAt(0); char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
// if we want full Big Number support the contents of this return stringToNumber(string);
// `try` block can be replaced with:
// return stringToNumber(string);
if (isDecimalNotation(string)) {
Double d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else {
Long myLong = Long.valueOf(string);
if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue());
}
return myLong;
}
}
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }

View file

@ -26,6 +26,8 @@ SOFTWARE.
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator; import java.util.Iterator;
/** /**
@ -424,17 +426,20 @@ public class XML {
*/ */
// To maintain compatibility with the Android API, this method is a direct copy of // To maintain compatibility with the Android API, this method is a direct copy of
// the one in JSONObject. Changes made here should be reflected there. // the one in JSONObject. Changes made here should be reflected there.
// This method should not make calls out of the XML object.
public static Object stringToValue(String string) { public static Object stringToValue(String string) {
if (string.equals("")) { if ("".equals(string)) {
return string; return string;
} }
if (string.equalsIgnoreCase("true")) {
// check JSON key words true/false/null
if ("true".equalsIgnoreCase(string)) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if (string.equalsIgnoreCase("false")) { if ("false".equalsIgnoreCase(string)) {
return Boolean.FALSE; return Boolean.FALSE;
} }
if (string.equalsIgnoreCase("null")) { if ("null".equalsIgnoreCase(string)) {
return JSONObject.NULL; return JSONObject.NULL;
} }
@ -446,28 +451,84 @@ public class XML {
char initial = string.charAt(0); char initial = string.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') { if ((initial >= '0' && initial <= '9') || initial == '-') {
try { try {
// if we want full Big Number support this block can be replaced with: return stringToNumber(string);
// return stringToNumber(string);
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|| string.indexOf('E') > -1 || "-0".equals(string)) {
Double d = Double.valueOf(string);
if (!d.isInfinite() && !d.isNaN()) {
return d;
}
} else {
Long myLong = Long.valueOf(string);
if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue());
}
return myLong;
}
}
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
return string; return string;
} }
/**
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
*/
private static Number stringToNumber(final String val) throws NumberFormatException {
char initial = val.charAt(0);
if ((initial >= '0' && initial <= '9') || initial == '-') {
// decimal representation
if (isDecimalNotation(val)) {
// Use a BigDecimal all the time so we keep the original
// representation. BigDecimal doesn't support -0.0, ensure we
// keep that by forcing a decimal.
try {
BigDecimal bd = new BigDecimal(val);
if(initial == '-' && BigDecimal.ZERO.compareTo(bd)==0) {
return Double.valueOf(-0.0);
}
return bd;
} catch (NumberFormatException retryAsDouble) {
// this is to support "Hex Floats" like this: 0x1.0P-1074
try {
Double d = Double.valueOf(val);
if(d.isNaN() || d.isInfinite()) {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
return d;
} catch (NumberFormatException ignore) {
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
}
}
// block items like 00 01 etc. Java number parsers treat these as Octal.
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.");
}
} 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.");
}
}
// integer representation.
// This will narrow any values to the smallest reasonable Object representation
// (Integer, Long, or BigInteger)
// BigInteger down conversion: We use a similar bitLenth compare as
// BigInteger#intValueExact uses. Increases GC, but objects hold
// only what they need. i.e. Less runtime overhead if the value is
// long lived.
BigInteger bi = new BigInteger(val);
if(bi.bitLength() <= 31){
return Integer.valueOf(bi.intValue());
}
if(bi.bitLength() <= 63){
return Long.valueOf(bi.longValue());
}
return bi;
}
throw new NumberFormatException("val ["+val+"] is not a valid number.");
}
/**
* direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
*/
private static boolean isDecimalNotation(final String val) {
return val.indexOf('.') > -1 || val.indexOf('e') > -1
|| val.indexOf('E') > -1 || "-0".equals(val);
}
/** /**
* Convert a well-formed (but not necessarily valid) XML string into a * Convert a well-formed (but not necessarily valid) XML string into a

View file

@ -33,7 +33,14 @@ import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -424,7 +431,7 @@ public class JSONArrayTest {
assertTrue("expected \"true\"", "true".equals(jsonArray.query("/2"))); assertTrue("expected \"true\"", "true".equals(jsonArray.query("/2")));
assertTrue("expected \"false\"", "false".equals(jsonArray.query("/3"))); assertTrue("expected \"false\"", "false".equals(jsonArray.query("/3")));
assertTrue("expected hello", "hello".equals(jsonArray.query("/4"))); assertTrue("expected hello", "hello".equals(jsonArray.query("/4")));
assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(jsonArray.query("/5"))); assertTrue("expected 0.002345", BigDecimal.valueOf(0.002345).equals(jsonArray.query("/5")));
assertTrue("expected \"23.45\"", "23.45".equals(jsonArray.query("/6"))); assertTrue("expected \"23.45\"", "23.45".equals(jsonArray.query("/6")));
assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/7"))); assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/7")));
assertTrue("expected \"43\"", "43".equals(jsonArray.query("/8"))); assertTrue("expected \"43\"", "43".equals(jsonArray.query("/8")));
@ -491,7 +498,7 @@ public class JSONArrayTest {
new Float(jsonArray.optFloat(99)).isNaN()); new Float(jsonArray.optFloat(99)).isNaN());
assertTrue("Array opt Number", assertTrue("Array opt Number",
new Double(23.45e-4).equals(jsonArray.optNumber(5))); BigDecimal.valueOf(23.45e-4).equals(jsonArray.optNumber(5)));
assertTrue("Array opt Number default", assertTrue("Array opt Number default",
new Double(1).equals(jsonArray.optNumber(0, 1d))); new Double(1).equals(jsonArray.optNumber(0, 1d)));
assertTrue("Array opt Number default implicit", assertTrue("Array opt Number default implicit",
@ -832,13 +839,6 @@ public class JSONArrayTest {
for (String s : jsonArray4Strs) { for (String s : jsonArray4Strs) {
list.contains(s); list.contains(s);
} }
// assertEquals("Expected same number of lines", actualStrArray.length,
// jsonArray0Strs.length);
// assertEquals(jsonArray0Str, jsonArray.toString());
// assertEquals(jsonArray0Str, jsonArray.toString(0));
// assertEquals(jsonArray1Str, jsonArray.toString(1));
// assertEquals(jsonArray4Str, jsonArray.toString(4));
} }
/** /**
@ -878,7 +878,7 @@ public class JSONArrayTest {
*/ */
@SuppressWarnings("boxing") @SuppressWarnings("boxing")
@Test @Test
public void iterator() { public void iteratorTest() {
JSONArray jsonArray = new JSONArray(this.arrayStr); JSONArray jsonArray = new JSONArray(this.arrayStr);
Iterator<Object> it = jsonArray.iterator(); Iterator<Object> it = jsonArray.iterator();
assertTrue("Array true", assertTrue("Array true",
@ -892,8 +892,8 @@ public class JSONArrayTest {
assertTrue("Array string", assertTrue("Array string",
"hello".equals(it.next())); "hello".equals(it.next()));
assertTrue("Array double", assertTrue("Array double [23.45e-4]",
new Double(23.45e-4).equals(it.next())); new BigDecimal("0.002345").equals(it.next()));
assertTrue("Array string double", assertTrue("Array string double",
new Double(23.45).equals(Double.parseDouble((String)it.next()))); new Double(23.45).equals(Double.parseDouble((String)it.next())));
@ -939,24 +939,18 @@ public class JSONArrayTest {
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]"; String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
JSONArray jsonArray = new JSONArray(str); JSONArray jsonArray = new JSONArray(str);
String expectedStr = str; String expectedStr = str;
StringWriter stringWriter = new StringWriter(); try (StringWriter stringWriter = new StringWriter();) {
try {
jsonArray.write(stringWriter); jsonArray.write(stringWriter);
String actualStr = stringWriter.toString(); String actualStr = stringWriter.toString();
JSONArray finalArray = new JSONArray(actualStr); JSONArray finalArray = new JSONArray(actualStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray); Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
assertTrue("write() expected " + expectedStr + assertTrue("write() expected " + expectedStr +
" but found " + actualStr, " but found " + actualStr,
actualStr.contains("value1") && actualStr.startsWith("[\"value1\",\"value2\",{")
actualStr.contains("value2") && && actualStr.contains("\"key1\":1")
actualStr.contains("key1") && && actualStr.contains("\"key2\":2")
actualStr.contains("1") && && actualStr.contains("\"key3\":3")
actualStr.contains("key2") && );
actualStr.contains("2") &&
actualStr.contains("key3") &&
actualStr.contains("3"));
} finally {
stringWriter.close();
} }
} }
@ -986,41 +980,33 @@ public class JSONArrayTest {
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]"; String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
JSONArray jsonArray = new JSONArray(str0); JSONArray jsonArray = new JSONArray(str0);
String expectedStr = str0; String expectedStr = str0;
StringWriter stringWriter = new StringWriter(); try (StringWriter stringWriter = new StringWriter();) {
try {
String actualStr = jsonArray.write(stringWriter, 0, 0).toString(); String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
JSONArray finalArray = new JSONArray(actualStr); JSONArray finalArray = new JSONArray(actualStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray); Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
assertTrue("write() expected " + expectedStr + assertTrue("write() expected " + expectedStr +
" but found " + actualStr, " but found " + actualStr,
actualStr.contains("value1") && actualStr.startsWith("[\"value1\",\"value2\",{")
actualStr.contains("value2") && && actualStr.contains("\"key1\":1")
actualStr.contains("key1") && && actualStr.contains("\"key2\":false")
actualStr.contains("1") && && actualStr.contains("\"key3\":3.14")
actualStr.contains("key2") && );
actualStr.contains("false") &&
actualStr.contains("key3") &&
actualStr.contains("3.14"));
} finally {
stringWriter.close();
} }
stringWriter = new StringWriter();
try { try (StringWriter stringWriter = new StringWriter();) {
String actualStr = jsonArray.write(stringWriter, 2, 1).toString(); String actualStr = jsonArray.write(stringWriter, 2, 1).toString();
JSONArray finalArray = new JSONArray(actualStr); JSONArray finalArray = new JSONArray(actualStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray); Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
assertTrue("write() expected " + expectedStr + assertTrue("write() expected " + expectedStr +
" but found " + actualStr, " but found " + actualStr,
actualStr.contains("value1") && actualStr.startsWith("[\n" +
actualStr.contains("value2") && " \"value1\",\n" +
actualStr.contains("key1") && " \"value2\",\n" +
actualStr.contains("1") && " {")
actualStr.contains("key2") && && actualStr.contains("\"key1\": 1")
actualStr.contains("false") && && actualStr.contains("\"key2\": false")
actualStr.contains("key3") && && actualStr.contains("\"key3\": 3.14")
actualStr.contains("3.14")); );
} finally {
stringWriter.close();
} }
} }
@ -1118,7 +1104,7 @@ public class JSONArrayTest {
assertTrue("val3 list val 1 should not be null", val3Val1List != null); assertTrue("val3 list val 1 should not be null", val3Val1List != null);
assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2); assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2);
assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1")); assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1"));
assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1"))); assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(new BigDecimal("2.1")));
List<?> val3Val2List = (List<?>)val3List.get(1); List<?> val3Val2List = (List<?>)val3List.get(1);
assertTrue("val3 list val 2 should not be null", val3Val2List != null); assertTrue("val3 list val 2 should not be null", val3Val2List != null);

View file

@ -75,7 +75,6 @@ import org.json.junit.data.Singleton;
import org.json.junit.data.SingletonEnum; import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList; import org.json.junit.data.WeirdList;
import org.junit.Test; import org.junit.Test;
import org.junit.Ignore;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
@ -268,7 +267,7 @@ public class JSONObjectTest {
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey"))); assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey")));
assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey"))); assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey")));
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey"))); assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey")));
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObjectByName.query("/doubleKey"))); assertTrue("expected \"doubleKey\":-23.45e67", new BigDecimal("-23.45e67").equals(jsonObjectByName.query("/doubleKey")));
} }
/** /**
@ -891,14 +890,14 @@ public class JSONObjectTest {
jsonObject.optNumber("intKey") instanceof Integer); jsonObject.optNumber("intKey") instanceof Integer);
assertTrue("optNumber long should return Long", assertTrue("optNumber long should return Long",
jsonObject.optNumber("longKey") instanceof Long); jsonObject.optNumber("longKey") instanceof Long);
assertTrue("optNumber double should return Double", assertTrue("optNumber double should return BigDecimal",
jsonObject.optNumber("doubleKey") instanceof Double); jsonObject.optNumber("doubleKey") instanceof BigDecimal);
assertTrue("optNumber Str int should return Integer", assertTrue("optNumber Str int should return Integer",
jsonObject.optNumber("intStrKey") instanceof Integer); jsonObject.optNumber("intStrKey") instanceof Integer);
assertTrue("optNumber Str long should return Long", assertTrue("optNumber Str long should return Long",
jsonObject.optNumber("longStrKey") instanceof Long); jsonObject.optNumber("longStrKey") instanceof Long);
assertTrue("optNumber Str double should return Double", assertTrue("optNumber Str double should return BigDecimal",
jsonObject.optNumber("doubleStrKey") instanceof Double); jsonObject.optNumber("doubleStrKey") instanceof BigDecimal);
assertTrue("optNumber BigDecimalStrKey should return BigDecimal", assertTrue("optNumber BigDecimalStrKey should return BigDecimal",
jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal); jsonObject.optNumber("BigDecimalStrKey") instanceof BigDecimal);
assertTrue("xKey should not exist", assertTrue("xKey should not exist",
@ -933,27 +932,27 @@ public class JSONObjectTest {
assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double); assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double);
assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String); assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String);
assertTrue( "0.2 should be a Double!", assertTrue( "0.2 should be a Double!",
JSONObject.stringToValue( "0.2" ) instanceof Double ); JSONObject.stringToValue( "0.2" ) instanceof BigDecimal );
assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!", assertTrue( "Doubles should be BigDecimal, even when incorrectly converting floats!",
JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof Double ); JSONObject.stringToValue( new Double( "0.2f" ).toString() ) instanceof BigDecimal );
/** /**
* This test documents a need for BigDecimal conversion. * This test documents a need for BigDecimal conversion.
*/ */
Object obj = JSONObject.stringToValue( "299792.457999999984" ); Object obj = JSONObject.stringToValue( "299792.457999999984" );
assertTrue( "evaluates to 299792.458 double instead of 299792.457999999984 BigDecimal!", assertTrue( "does not evaluate to 299792.457999999984 BigDecimal!",
obj.equals(new Double(299792.458)) ); obj.equals(new BigDecimal("299792.457999999984")) );
assertTrue( "1 should be an Integer!", assertTrue( "1 should be an Integer!",
JSONObject.stringToValue( "1" ) instanceof Integer ); JSONObject.stringToValue( "1" ) instanceof Integer );
assertTrue( "Integer.MAX_VALUE should still be an Integer!", assertTrue( "Integer.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer ); JSONObject.stringToValue( new Integer( Integer.MAX_VALUE ).toString() ) instanceof Integer );
// assertTrue( "Large integers should be a Long!", assertTrue( "Large integers should be a Long!",
// JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long ); JSONObject.stringToValue( new Long( Long.sum( Integer.MAX_VALUE, 1 ) ).toString() ) instanceof Long );
assertTrue( "Long.MAX_VALUE should still be an Integer!", assertTrue( "Long.MAX_VALUE should still be an Integer!",
JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long ); JSONObject.stringToValue( new Long( Long.MAX_VALUE ).toString() ) instanceof Long );
String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString(); String str = new BigInteger( new Long( Long.MAX_VALUE ).toString() ).add( BigInteger.ONE ).toString();
assertTrue( "Really large integers currently evaluate to string", assertTrue( "Really large integers currently evaluate to BigInteger",
JSONObject.stringToValue(str).equals("9223372036854775808")); JSONObject.stringToValue(str).equals(new BigInteger("9223372036854775808")));
} }
/** /**
@ -974,16 +973,16 @@ public class JSONObjectTest {
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
// Comes back as a double, but loses precision // Comes back as a double, but loses precision
assertTrue( "numberWithDecimals currently evaluates to double 299792.458", assertTrue( "numberWithDecimals currently evaluates to double 299792.458",
jsonObject.get( "numberWithDecimals" ).equals( new Double( "299792.458" ) ) ); jsonObject.get( "numberWithDecimals" ).equals( new BigDecimal( "299792.457999999984" ) ) );
Object obj = jsonObject.get( "largeNumber" ); Object obj = jsonObject.get( "largeNumber" );
assertTrue("largeNumber currently evaluates to string", assertTrue("largeNumber currently evaluates to BigInteger",
"12345678901234567890".equals(obj)); new BigInteger("12345678901234567890").equals(obj));
// comes back as a double but loses precision // comes back as a double but loses precision
assertTrue( "preciseNumber currently evaluates to double 0.2", assertEquals( "preciseNumber currently evaluates to double 0.2",
jsonObject.get( "preciseNumber" ).equals(new Double(0.2))); 0.2, jsonObject.getDouble( "preciseNumber" ), 0.0);
obj = jsonObject.get( "largeExponent" ); obj = jsonObject.get( "largeExponent" );
assertTrue("largeExponent should currently evaluates as a string", assertTrue("largeExponent should evaluate as a BigDecimal",
"-23.45e2327".equals(obj)); new BigDecimal("-23.45e2327").equals(obj));
} }
/** /**
@ -1021,17 +1020,17 @@ public class JSONObjectTest {
assertTrue( "negativeNaN currently evaluates to string", assertTrue( "negativeNaN currently evaluates to string",
obj.equals("-NaN")); obj.equals("-NaN"));
assertTrue( "negativeFraction currently evaluates to double -0.01", assertTrue( "negativeFraction currently evaluates to double -0.01",
jsonObject.get( "negativeFraction" ).equals(new Double(-0.01))); jsonObject.get( "negativeFraction" ).equals(BigDecimal.valueOf(-0.01)));
assertTrue( "tooManyZerosFraction currently evaluates to double 0.001", assertTrue( "tooManyZerosFraction currently evaluates to double 0.001",
jsonObject.get( "tooManyZerosFraction" ).equals(new Double(0.001))); jsonObject.get( "tooManyZerosFraction" ).equals(BigDecimal.valueOf(0.001)));
assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875", assertTrue( "negativeHexFloat currently evaluates to double -3.99951171875",
jsonObject.get( "negativeHexFloat" ).equals(new Double(-3.99951171875))); jsonObject.get( "negativeHexFloat" ).equals(Double.valueOf(-3.99951171875)));
assertTrue("hexFloat currently evaluates to double 4.9E-324", assertTrue("hexFloat currently evaluates to double 4.9E-324",
jsonObject.get("hexFloat").equals(new Double(4.9E-324))); jsonObject.get("hexFloat").equals(Double.valueOf(4.9E-324)));
assertTrue("floatIdentifier currently evaluates to double 0.1", assertTrue("floatIdentifier currently evaluates to double 0.1",
jsonObject.get("floatIdentifier").equals(new Double(0.1))); jsonObject.get("floatIdentifier").equals(Double.valueOf(0.1)));
assertTrue("doubleIdentifier currently evaluates to double 0.1", assertTrue("doubleIdentifier currently evaluates to double 0.1",
jsonObject.get("doubleIdentifier").equals(new Double(0.1))); jsonObject.get("doubleIdentifier").equals(Double.valueOf(0.1)));
} }
/** /**
@ -1311,11 +1310,11 @@ public class JSONObjectTest {
* might inconvenience users. * might inconvenience users.
*/ */
obj = JSONObject.stringToValue(bigInteger.toString()); obj = JSONObject.stringToValue(bigInteger.toString());
assertTrue("stringToValue() turns bigInteger string into string", assertTrue("stringToValue() turns bigInteger string into Number",
obj instanceof String); obj instanceof Number);
obj = JSONObject.stringToValue(bigDecimal.toString()); obj = JSONObject.stringToValue(bigDecimal.toString());
assertTrue("stringToValue() changes bigDecimal string", assertTrue("stringToValue() changes bigDecimal Number",
!obj.toString().equals(bigDecimal.toString())); obj instanceof Number);
/** /**
* wrap() vs put() big number behavior is now the same. * wrap() vs put() big number behavior is now the same.
@ -1580,7 +1579,7 @@ public class JSONObjectTest {
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6); assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt"))); assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt")));
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong"))); assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong")));
assertTrue("expected 3.1", Double.valueOf(3.1).equals(jsonObject.query("/keyDouble"))); assertTrue("expected 3.1", BigDecimal.valueOf(3.1).equals(jsonObject.query("/keyDouble")));
assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt"))); assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt")));
assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec"))); assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec")));
@ -2083,7 +2082,7 @@ public class JSONObjectTest {
/** /**
* Explore how JSONObject handles parsing errors. * Explore how JSONObject handles parsing errors.
*/ */
@SuppressWarnings("boxing") @SuppressWarnings({"boxing", "unused"})
@Test @Test
public void jsonObjectParsingErrors() { public void jsonObjectParsingErrors() {
try { try {
@ -2629,14 +2628,11 @@ public class JSONObjectTest {
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}"; String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
String expectedStr = str; String expectedStr = str;
JSONObject jsonObject = new JSONObject(str); JSONObject jsonObject = new JSONObject(str);
StringWriter stringWriter = new StringWriter(); try (StringWriter stringWriter = new StringWriter()) {
try {
String actualStr = jsonObject.write(stringWriter).toString(); String actualStr = jsonObject.write(stringWriter).toString();
assertTrue("write() expected " +expectedStr+ assertTrue("write() expected " +expectedStr+
" but found " +actualStr, " but found " +actualStr,
expectedStr.equals(actualStr)); expectedStr.equals(actualStr));
} finally {
stringWriter.close();
} }
} }
@ -2739,21 +2735,15 @@ public class JSONObjectTest {
" }"; " }";
JSONObject jsonObject = new JSONObject(str0); JSONObject jsonObject = new JSONObject(str0);
String expectedStr = str0; String expectedStr = str0;
StringWriter stringWriter = new StringWriter(); try (StringWriter stringWriter = new StringWriter();) {
try {
String actualStr = jsonObject.write(stringWriter,0,0).toString(); String actualStr = jsonObject.write(stringWriter,0,0).toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
} finally {
stringWriter.close();
} }
expectedStr = str2; expectedStr = str2;
stringWriter = new StringWriter(); try (StringWriter stringWriter = new StringWriter();) {
try {
String actualStr = jsonObject.write(stringWriter,2,1).toString(); String actualStr = jsonObject.write(stringWriter,2,1).toString();
assertEquals(expectedStr, actualStr); assertEquals(expectedStr, actualStr);
} finally {
stringWriter.close();
} }
} }
@ -2965,7 +2955,7 @@ public class JSONObjectTest {
assertTrue("key3 list val 1 should not be null", key3Val1List != null); assertTrue("key3 list val 1 should not be null", key3Val1List != null);
assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2); assertTrue("key3 list val 1 should have 2 elements", key3Val1List.size() == 2);
assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1")); assertTrue("key3 list val 1 list element 1 should be value1", key3Val1List.get(0).equals("value1"));
assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(Double.valueOf("2.1"))); assertTrue("key3 list val 1 list element 2 should be 2.1", key3Val1List.get(1).equals(new BigDecimal("2.1")));
List<?> key3Val2List = (List<?>)key3List.get(1); List<?> key3Val2List = (List<?>)key3List.get(1);
assertTrue("key3 list val 2 should not be null", key3Val2List != null); assertTrue("key3 list val 2 should not be null", key3Val2List != null);
@ -2984,8 +2974,7 @@ public class JSONObjectTest {
/** /**
* test that validates a singleton can be serialized as a bean. * test that validates a singleton can be serialized as a bean.
*/ */
// @todo: investigate, re-enable this test @SuppressWarnings("boxing")
@Ignore
@Test @Test
public void testSingletonBean() { public void testSingletonBean() {
final JSONObject jo = new JSONObject(Singleton.getInstance()); final JSONObject jo = new JSONObject(Singleton.getInstance());
@ -3009,8 +2998,7 @@ public class JSONObjectTest {
/** /**
* test that validates a singleton can be serialized as a bean. * test that validates a singleton can be serialized as a bean.
*/ */
// @todo: investigate, re-enable this test @SuppressWarnings("boxing")
@Ignore
@Test @Test
public void testSingletonEnumBean() { public void testSingletonEnumBean() {
final JSONObject jo = new JSONObject(SingletonEnum.getInstance()); final JSONObject jo = new JSONObject(SingletonEnum.getInstance());
@ -3034,6 +3022,7 @@ public class JSONObjectTest {
/** /**
* Test to validate that a generic class can be serialized as a bean. * Test to validate that a generic class can be serialized as a bean.
*/ */
@SuppressWarnings("boxing")
@Test @Test
public void testGenericBean() { public void testGenericBean() {
GenericBean<Integer> bean = new GenericBean<>(42); GenericBean<Integer> bean = new GenericBean<>(42);
@ -3048,6 +3037,7 @@ public class JSONObjectTest {
/** /**
* Test to validate that a generic class can be serialized as a bean. * Test to validate that a generic class can be serialized as a bean.
*/ */
@SuppressWarnings("boxing")
@Test @Test
public void testGenericIntBean() { public void testGenericIntBean() {
GenericBeanInt bean = new GenericBeanInt(42); GenericBeanInt bean = new GenericBeanInt(42);
@ -3064,6 +3054,7 @@ public class JSONObjectTest {
*/ */
@Test @Test
public void testWierdListBean() { public void testWierdListBean() {
@SuppressWarnings("boxing")
WeirdList bean = new WeirdList(42, 43, 44); WeirdList bean = new WeirdList(42, 43, 44);
final JSONObject jo = new JSONObject(bean); final JSONObject jo = new JSONObject(bean);
// get() should have a key of 0 length // get() should have a key of 0 length

View file

@ -29,7 +29,6 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.json.JSONArray; import org.json.JSONArray;
@ -61,6 +60,7 @@ public class JSONPointerTest {
assertSame(document, query("")); assertSame(document, query(""));
} }
@SuppressWarnings("unused")
@Test(expected = NullPointerException.class) @Test(expected = NullPointerException.class)
public void nullPointer() { public void nullPointer() {
new JSONPointer((String) null); new JSONPointer((String) null);
@ -150,6 +150,7 @@ public class JSONPointerTest {
assertSame(document.get("m~n"), query("#/m~0n")); assertSame(document.get("m~n"), query("#/m~0n"));
} }
@SuppressWarnings("unused")
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void syntaxError() { public void syntaxError() {
new JSONPointer("key"); new JSONPointer("key");

View file

@ -26,7 +26,6 @@ SOFTWARE.
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.*; import java.util.*;
@ -50,105 +49,84 @@ public class JSONStringTest {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
jsonArray.put((Object)null); jsonArray.put((Object)null);
StringWriter writer = new StringWriter(); try (StringWriter writer = new StringWriter();) {
try {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[null]".equals(output)); assertTrue("String values should be equal", "[null]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
jsonArray.put(JSONObject.NULL); jsonArray.put(JSONObject.NULL);
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[null]".equals(output)); assertTrue("String values should be equal", "[null]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
jsonArray.put(new JSONObject()); jsonArray.put(new JSONObject());
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[{}]".equals(output)); assertTrue("String values should be equal", "[{}]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
jsonArray.put(new JSONArray()); jsonArray.put(new JSONArray());
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[[]]".equals(output)); assertTrue("String values should be equal", "[[]]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
Map<?,?> singleMap = Collections.singletonMap("key1", "value1"); Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
jsonArray.put((Object)singleMap); jsonArray.put((Object)singleMap);
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output)); assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
List<?> singleList = Collections.singletonList("entry1"); List<?> singleList = Collections.singletonList("entry1");
jsonArray.put((Object)singleList); jsonArray.put((Object)singleList);
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output)); assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
int[] intArray = new int[] { 1, 2, 3 }; int[] intArray = new int[] { 1, 2, 3 };
jsonArray.put(intArray); jsonArray.put(intArray);
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[[1,2,3]]".equals(output)); assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
jsonArray.put(24); jsonArray.put(24);
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[24]".equals(output)); assertTrue("String values should be equal", "[24]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
jsonArray.put("string value"); jsonArray.put("string value");
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[\"string value\"]".equals(output)); assertTrue("String values should be equal", "[\"string value\"]".equals(output));
jsonArray = new JSONArray(); jsonArray = new JSONArray();
jsonArray.put(true); jsonArray.put(true);
} finally {
writer.close();
} }
writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[true]".equals(output)); assertTrue("String values should be equal", "[true]".equals(output));
} finally {
writer.close();
} }
} }
@ -207,15 +185,13 @@ public class JSONStringTest {
jsonArray.put(jsonString); jsonArray.put(jsonString);
StringWriter writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output)); assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
output = JSONObject.valueToString(jsonString); output = JSONObject.valueToString(jsonString);
assertTrue("String values should be equal", "\"the JSON string value\"".equals(output)); assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
} finally {
writer.close();
} }
} }
@ -230,8 +206,7 @@ public class JSONStringTest {
jsonArray.put(jsonString); jsonArray.put(jsonString);
StringWriter writer = new StringWriter(); try (StringWriter writer = new StringWriter();) {
try {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
@ -244,8 +219,6 @@ public class JSONStringTest {
assertTrue("Expected JSONException", e instanceof JSONException); assertTrue("Expected JSONException", e instanceof JSONException);
assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage())); assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
} }
} finally {
writer.close();
} }
} }
@ -255,22 +228,19 @@ public class JSONStringTest {
* the original exception. * the original exception.
*/ */
@Test @Test
public void testJSONStringExceptionValue() throws IOException { public void testJSONStringExceptionValue() {
JSONStringExceptionValue jsonString = new JSONStringExceptionValue(); JSONStringExceptionValue jsonString = new JSONStringExceptionValue();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonString); jsonArray.put(jsonString);
StringWriter writer = new StringWriter(); try (StringWriter writer = new StringWriter();) {
try {
jsonArray.write(writer).toString(); jsonArray.write(writer).toString();
fail("Expected an exception, got a String value"); fail("Expected an exception, got a String value");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Unable to write JSONArray value at index: 0", e.getMessage()); assertEquals("Unable to write JSONArray value at index: 0", e.getMessage());
} catch(Exception e) { } catch(Exception e) {
fail("Expected JSONException"); fail("Expected JSONException");
} finally {
writer.close();
} }
try { try {
@ -294,15 +264,12 @@ public class JSONStringTest {
jsonArray.put(nonJsonString); jsonArray.put(nonJsonString);
StringWriter writer = new StringWriter(); try (StringWriter writer = new StringWriter();) {
try {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output)); assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
output = JSONObject.valueToString(nonJsonString); output = JSONObject.valueToString(nonJsonString);
assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output)); assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
} finally {
writer.close();
} }
} }
@ -317,15 +284,13 @@ public class JSONStringTest {
jsonArray.put(nonJsonString); jsonArray.put(nonJsonString);
StringWriter writer = new StringWriter();
try { try (StringWriter writer = new StringWriter();) {
String output = jsonArray.write(writer).toString(); String output = jsonArray.write(writer).toString();
assertTrue("String values should be equal", "[\"\"]".equals(output)); assertTrue("String values should be equal", "[\"\"]".equals(output));
output = JSONObject.valueToString(nonJsonString); output = JSONObject.valueToString(nonJsonString);
assertTrue("String values should be equal", "\"\"".equals(output)); assertTrue("String values should be equal", "\"\"".equals(output));
} finally {
writer.close();
} }
} }

View file

@ -26,6 +26,7 @@ SOFTWARE.
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.util.*; import java.util.*;
import org.json.*; import org.json.*;
@ -269,7 +270,7 @@ public class JSONStringerTest {
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue"))); assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue"))); assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue"))); assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue"))); assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
} }
/** /**
@ -298,7 +299,7 @@ public class JSONStringerTest {
assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/2"))); assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/2")));
assertTrue("expected hello world!", "hello world!".equals(jsonArray.query("/3"))); assertTrue("expected hello world!", "hello world!".equals(jsonArray.query("/3")));
assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/4"))); assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/4")));
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonArray.query("/5"))); assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonArray.query("/5")));
} }
/** /**
@ -355,7 +356,7 @@ public class JSONStringerTest {
assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue"))); assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue"))); assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue"))); assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue"))); assertTrue("expected -23.45e67", BigDecimal.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue"))); assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
assertTrue("expected v1", "v1".equals(jsonObject.query("/object2/k1"))); assertTrue("expected v1", "v1".equals(jsonObject.query("/object2/k1")));
assertTrue("expected v2", "v2".equals(jsonObject.query("/object2/k2"))); assertTrue("expected v2", "v2".equals(jsonObject.query("/object2/k2")));

View file

@ -7,8 +7,6 @@ import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import org.json.JSONObject;
/** /**
* Object for testing the exception handling in {@link JSONObject#populateMap}. * Object for testing the exception handling in {@link JSONObject#populateMap}.
* *
@ -56,7 +54,6 @@ public class ExceptionalBean {
/** /**
* @return a string * @return a string
*/ */
@SuppressWarnings("unused")
public String getString() { public String getString() {
return "Yup, it's closeable"; return "Yup, it's closeable";
} }

View file

@ -8,13 +8,13 @@ public class MyEnumClass {
private MyEnumField myEnumField; private MyEnumField myEnumField;
public MyEnum getMyEnum() { public MyEnum getMyEnum() {
return myEnum; return this.myEnum;
} }
public void setMyEnum(MyEnum myEnum) { public void setMyEnum(MyEnum myEnum) {
this.myEnum = myEnum; this.myEnum = myEnum;
} }
public MyEnumField getMyEnumField() { public MyEnumField getMyEnumField() {
return myEnumField; return this.myEnumField;
} }
public void setMyEnumField(MyEnumField myEnumField) { public void setMyEnumField(MyEnumField myEnumField) {
this.myEnumField = myEnumField; this.myEnumField = myEnumField;

View file

@ -4,9 +4,9 @@ public class MyLocaleBean {
private final String id = "beanId"; private final String id = "beanId";
private final String i = "beanI"; private final String i = "beanI";
public String getId() { public String getId() {
return id; return this.id;
} }
public String getI() { public String getI() {
return i; return this.i;
} }
} }

View file

@ -33,7 +33,7 @@ public final class Singleton {
/** @return someInt */ /** @return someInt */
public int getSomeInt() { public int getSomeInt() {
return someInt; return this.someInt;
} }
/** /**
@ -48,7 +48,7 @@ public final class Singleton {
/** @return someString */ /** @return someString */
public String getSomeString() { public String getSomeString() {
return someString; return this.someString;
} }
/** /**
@ -65,8 +65,8 @@ public final class Singleton {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + someInt; result = prime * result + this.someInt;
result = prime * result + ((someString == null) ? 0 : someString.hashCode()); result = prime * result + ((this.someString == null) ? 0 : this.someString.hashCode());
return result; return result;
} }
@ -79,12 +79,12 @@ public final class Singleton {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Singleton other = (Singleton) obj; Singleton other = (Singleton) obj;
if (someInt != other.someInt) if (this.someInt != other.someInt)
return false; return false;
if (someString == null) { if (this.someString == null) {
if (other.someString != null) if (other.someString != null)
return false; return false;
} else if (!someString.equals(other.someString)) } else if (!this.someString.equals(other.someString))
return false; return false;
return true; return true;
} }

View file

@ -19,7 +19,7 @@ public enum SingletonEnum {
/** single instance. */ /** single instance. */
/** /**
* @return the singleton instance. I a real application, I'd hope no one did * @return the singleton instance. In a real application, I'd hope no one did
* this to an enum singleton. * this to an enum singleton.
*/ */
public static final SingletonEnum getInstance() { public static final SingletonEnum getInstance() {
@ -32,7 +32,7 @@ public enum SingletonEnum {
/** @return someInt */ /** @return someInt */
public int getSomeInt() { public int getSomeInt() {
return someInt; return this.someInt;
} }
/** /**
@ -47,7 +47,7 @@ public enum SingletonEnum {
/** @return someString */ /** @return someString */
public String getSomeString() { public String getSomeString() {
return someString; return this.someString;
} }
/** /**

View file

@ -53,6 +53,7 @@ public class WeirdList {
* index to get * index to get
* @return the value at the index * @return the value at the index
*/ */
@SuppressWarnings("boxing")
public int getInt(int i) { public int getInt(int i) {
return this.list.get(i); return this.list.get(i);
} }