diff options
author | Jesse Wilson <jessewilson@google.com> | 2011-07-08 10:37:55 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2011-07-08 10:54:54 -0700 |
commit | 661054f5a2f7f8f5f3ceffb97e803211b546e7fc (patch) | |
tree | 105d293bfe5e5b93c76b44679de89d0d6d3b1fed /json | |
parent | 6611bab7cb4ace7320a0584c4777f86a0da17e72 (diff) | |
download | libcore-661054f5a2f7f8f5f3ceffb97e803211b546e7fc.zip libcore-661054f5a2f7f8f5f3ceffb97e803211b546e7fc.tar.gz libcore-661054f5a2f7f8f5f3ceffb97e803211b546e7fc.tar.bz2 |
Fix string to boolean coercsion.
When we reimplemented this API I broke consistency with
org.json. Bringing it back into line makes the implementation
more strict (my preference) and makes us consistent with
ourselves before this code was redone.
Bug: http://code.google.com/p/android/issues/detail?id=16411
Change-Id: I8c1b52e382ad91932d3cf9a5b346db58df4da7c6
Diffstat (limited to 'json')
-rw-r--r-- | json/src/main/java/org/json/JSON.java | 18 | ||||
-rw-r--r-- | json/src/main/java/org/json/JSONException.java | 2 | ||||
-rw-r--r-- | json/src/main/java/org/json/JSONObject.java | 6 | ||||
-rw-r--r-- | json/src/main/java/org/json/JSONStringer.java | 2 | ||||
-rw-r--r-- | json/src/main/java/org/json/JSONTokener.java | 6 | ||||
-rw-r--r-- | json/src/test/java/org/json/JSONArrayTest.java | 13 | ||||
-rw-r--r-- | json/src/test/java/org/json/JSONObjectTest.java | 13 |
7 files changed, 45 insertions, 15 deletions
diff --git a/json/src/main/java/org/json/JSON.java b/json/src/main/java/org/json/JSON.java index b32124d..1b32e69 100644 --- a/json/src/main/java/org/json/JSON.java +++ b/json/src/main/java/org/json/JSON.java @@ -18,7 +18,7 @@ package org.json; class JSON { /** - * Returns the input if it is a JSON-permissable value; throws otherwise. + * Returns the input if it is a JSON-permissible value; throws otherwise. */ static double checkDouble(double d) throws JSONException { if (Double.isInfinite(d) || Double.isNaN(d)) { @@ -31,10 +31,14 @@ class JSON { if (value instanceof Boolean) { return (Boolean) value; } else if (value instanceof String) { - return Boolean.valueOf(((String) value)); - } else { - return null; + String stringValue = (String) value; + if ("true".equalsIgnoreCase(stringValue)) { + return true; + } else if ("false".equalsIgnoreCase(stringValue)) { + return false; + } } + return null; } static Double toDouble(Object value) { @@ -45,7 +49,7 @@ class JSON { } else if (value instanceof String) { try { return Double.valueOf((String) value); - } catch (NumberFormatException e) { + } catch (NumberFormatException ignored) { } } return null; @@ -59,7 +63,7 @@ class JSON { } else if (value instanceof String) { try { return (int) Double.parseDouble((String) value); - } catch (NumberFormatException e) { + } catch (NumberFormatException ignored) { } } return null; @@ -73,7 +77,7 @@ class JSON { } else if (value instanceof String) { try { return (long) Double.parseDouble((String) value); - } catch (NumberFormatException e) { + } catch (NumberFormatException ignored) { } } return null; diff --git a/json/src/main/java/org/json/JSONException.java b/json/src/main/java/org/json/JSONException.java index ddd1016..2c66eb8 100644 --- a/json/src/main/java/org/json/JSONException.java +++ b/json/src/main/java/org/json/JSONException.java @@ -25,7 +25,7 @@ package org.json; * <li>Use of null as a name * <li>Use of numeric types not available to JSON, such as {@link * Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}. - * <li>Lookups using an out of range index or nonexistant name + * <li>Lookups using an out of range index or nonexistent name * <li>Type mismatches on lookups * </ul> * diff --git a/json/src/main/java/org/json/JSONObject.java b/json/src/main/java/org/json/JSONObject.java index 56c91cf..e7ca735 100644 --- a/json/src/main/java/org/json/JSONObject.java +++ b/json/src/main/java/org/json/JSONObject.java @@ -32,8 +32,8 @@ import java.util.Map; * * <p>This class can coerce values to another type when requested. * <ul> - * <li>When the requested type is a boolean, strings will be coerced - * using {@link Boolean#valueOf(String)}. + * <li>When the requested type is a boolean, strings will be coerced using a + * case-insensitive comparison to "true" and "false". * <li>When the requested type is a double, other {@link Number} types will * be coerced using {@link Number#doubleValue() doubleValue}. Strings * that can be coerced using {@link Double#valueOf(String)} will be. @@ -71,7 +71,7 @@ import java.util.Map; * * <p>Instances of this class are not thread safe. Although this class is * nonfinal, it was not designed for inheritance and should not be subclassed. - * In particular, self-use by overridable methods is not specified. See + * In particular, self-use by overrideable methods is not specified. See * <i>Effective Java</i> Item 17, "Design and Document or inheritance or else * prohibit it" for further information. */ diff --git a/json/src/main/java/org/json/JSONStringer.java b/json/src/main/java/org/json/JSONStringer.java index 3891905..dd3b2a7 100644 --- a/json/src/main/java/org/json/JSONStringer.java +++ b/json/src/main/java/org/json/JSONStringer.java @@ -54,7 +54,7 @@ import java.util.List; * <p>Each stringer may be used to encode a single top level value. Instances of * this class are not thread safe. Although this class is nonfinal, it was not * designed for inheritance and should not be subclassed. In particular, - * self-use by overridable methods is not specified. See <i>Effective Java</i> + * self-use by overrideable methods is not specified. See <i>Effective Java</i> * Item 17, "Design and Document or inheritance or else prohibit it" for further * information. */ diff --git a/json/src/main/java/org/json/JSONTokener.java b/json/src/main/java/org/json/JSONTokener.java index b6ac04d..dbc8f58 100644 --- a/json/src/main/java/org/json/JSONTokener.java +++ b/json/src/main/java/org/json/JSONTokener.java @@ -46,7 +46,7 @@ package org.json; * <li>Hexadecimal integers prefixed with {@code 0x} or {@code 0X}. * <li>Octal integers prefixed with {@code 0}. * <li>Array elements separated by {@code ;}. - * <li>Unnecessary array separators. These are interpretted as if null was the + * <li>Unnecessary array separators. These are interpreted as if null was the * omitted value. * <li>Key-value pairs separated by {@code =} or {@code =>}. * <li>Key-value pairs separated by {@code ;}. @@ -55,7 +55,7 @@ package org.json; * <p>Each tokener may be used to parse a single JSON string. Instances of this * class are not thread safe. Although this class is nonfinal, it was not * designed for inheritance and should not be subclassed. In particular, - * self-use by overridable methods is not specified. See <i>Effective Java</i> + * self-use by overrideable methods is not specified. See <i>Effective Java</i> * Item 17, "Design and Document or inheritance or else prohibit it" for further * information. */ @@ -317,7 +317,7 @@ public class JSONTokener { /* ...next try to parse as a floating point... */ try { return Double.valueOf(literal); - } catch (NumberFormatException e) { + } catch (NumberFormatException ignored) { } /* ... finally give up. We have an unquoted string */ diff --git a/json/src/test/java/org/json/JSONArrayTest.java b/json/src/test/java/org/json/JSONArrayTest.java index 97e0f6b..1e72af2 100644 --- a/json/src/test/java/org/json/JSONArrayTest.java +++ b/json/src/test/java/org/json/JSONArrayTest.java @@ -121,6 +121,19 @@ public class JSONArrayTest extends TestCase { assertEquals(false, other.getBoolean(3)); } + // http://code.google.com/p/android/issues/detail?id=16411 + public void testCoerceStringToBoolean() throws JSONException { + JSONArray array = new JSONArray(); + array.put("maybe"); + try { + array.getBoolean(0); + fail(); + } catch (JSONException expected) { + } + assertEquals(false, array.optBoolean(0)); + assertEquals(true, array.optBoolean(0, true)); + } + public void testNulls() throws JSONException { JSONArray array = new JSONArray(); array.put(3, null); diff --git a/json/src/test/java/org/json/JSONObjectTest.java b/json/src/test/java/org/json/JSONObjectTest.java index d84acfc..e192b03 100644 --- a/json/src/test/java/org/json/JSONObjectTest.java +++ b/json/src/test/java/org/json/JSONObjectTest.java @@ -243,6 +243,19 @@ public class JSONObjectTest extends TestCase { assertEquals(false, object.optBoolean("bar", false)); } + // http://code.google.com/p/android/issues/detail?id=16411 + public void testCoerceStringToBoolean() throws JSONException { + JSONObject object = new JSONObject(); + object.put("foo", "maybe"); + try { + object.getBoolean("foo"); + fail(); + } catch (JSONException expected) { + } + assertEquals(false, object.optBoolean("foo")); + assertEquals(true, object.optBoolean("foo", true)); + } + public void testNumbers() throws JSONException { JSONObject object = new JSONObject(); object.put("foo", Double.MIN_VALUE); |