From 661054f5a2f7f8f5f3ceffb97e803211b546e7fc Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 8 Jul 2011 10:37:55 -0700 Subject: 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 --- json/src/main/java/org/json/JSON.java | 18 +++++++++++------- json/src/main/java/org/json/JSONException.java | 2 +- json/src/main/java/org/json/JSONObject.java | 6 +++--- json/src/main/java/org/json/JSONStringer.java | 2 +- json/src/main/java/org/json/JSONTokener.java | 6 +++--- 5 files changed, 19 insertions(+), 15 deletions(-) (limited to 'json/src/main/java') 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; *
  • Use of null as a name *
  • Use of numeric types not available to JSON, such as {@link * Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}. - *
  • Lookups using an out of range index or nonexistant name + *
  • Lookups using an out of range index or nonexistent name *
  • Type mismatches on lookups * * 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; * *

    This class can coerce values to another type when requested. *