Skip to content

Commit 71c1eba

Browse files
authored
Merge pull request #51 from johnjaylward/VerifyOptConversions
Verify opt method conversions for JSONArray and JSONObject
2 parents 01af317 + c2de224 commit 71c1eba

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.junit.Assert.assertNull;
44
import static org.junit.Assert.assertTrue;
55

6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
68
import java.util.ArrayList;
79
import java.util.Collection;
810
import java.util.Collections;
@@ -414,6 +416,20 @@ public void opt() {
414416
assertTrue("Array opt string default implicit",
415417
"".equals(jsonArray.optString(-1)));
416418
}
419+
420+
/**
421+
* Verifies that the opt methods properly convert string values.
422+
*/
423+
@Test
424+
public void optStringConversion(){
425+
JSONArray ja = new JSONArray("[\"123\",\"true\",\"false\"]");
426+
assertTrue("unexpected optBoolean value",ja.optBoolean(1,false)==true);
427+
assertTrue("unexpected optBoolean value",ja.optBoolean(2,true)==false);
428+
assertTrue("unexpected optInt value",ja.optInt(0,0)==123);
429+
assertTrue("unexpected optLong value",ja.optLong(0,0)==123);
430+
assertTrue("unexpected optDouble value",ja.optDouble(0,0.0)==123.0);
431+
assertTrue("unexpected optBigInteger value",ja.optBigInteger(0,BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
432+
assertTrue("unexpected optBigDecimal value",ja.optBigDecimal(0,BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0); }
417433

418434
/**
419435
* Exercise the JSONArray.put(value) method with various parameters

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,22 @@ public void jsonObjectOptDefault() {
17351735
assertTrue("optString() should return default string",
17361736
"hi".equals(jsonObject.optString("hiKey", "hi")));
17371737
}
1738+
1739+
/**
1740+
* Verifies that the opt methods properly convert string values.
1741+
*/
1742+
@Test
1743+
public void jsonObjectOptStringConversion() {
1744+
JSONObject jo = new JSONObject("{\"int\":\"123\",\"true\":\"true\",\"false\":\"false\"}");
1745+
assertTrue("unexpected optBoolean value",jo.optBoolean("true",false)==true);
1746+
assertTrue("unexpected optBoolean value",jo.optBoolean("false",true)==false);
1747+
assertTrue("unexpected optInt value",jo.optInt("int",0)==123);
1748+
assertTrue("unexpected optLong value",jo.optLong("int",0)==123);
1749+
assertTrue("unexpected optDouble value",jo.optDouble("int",0.0)==123.0);
1750+
assertTrue("unexpected optBigInteger value",jo.optBigInteger("int",BigInteger.ZERO).compareTo(new BigInteger("123"))==0);
1751+
assertTrue("unexpected optBigDecimal value",jo.optBigDecimal("int",BigDecimal.ZERO).compareTo(new BigDecimal("123"))==0);
1752+
1753+
}
17381754

17391755
/**
17401756
* Confirm behavior when JSONObject put(key, null object) is called

0 commit comments

Comments
 (0)