From 0340e01f98274f2ec573fcd1475fdc3d3fbca4ef Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Sun, 23 Oct 2011 21:41:30 +0000 Subject: [PATCH] Support booleans as strings in stream binding. Remove support for interpreting "1" as true. Interpreting '1' as true was not backwards compatible. --- gson/src/main/java/com/google/gson/JsonPrimitive.java | 7 ++----- .../java/com/google/gson/internal/bind/TypeAdapters.java | 3 +++ .../src/test/java/com/google/gson/JsonPrimitiveTest.java | 4 ++-- .../java/com/google/gson/functional/PrimitiveTest.java | 9 +++++++++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/gson/src/main/java/com/google/gson/JsonPrimitive.java b/gson/src/main/java/com/google/gson/JsonPrimitive.java index ad868c65..42463f8f 100644 --- a/gson/src/main/java/com/google/gson/JsonPrimitive.java +++ b/gson/src/main/java/com/google/gson/JsonPrimitive.java @@ -131,11 +131,8 @@ public final class JsonPrimitive extends JsonElement { if (isBoolean()) { return getAsBooleanWrapper().booleanValue(); } else { - // Check to see if the value (as a String) is either "true" (ignore case) or "1". - // If so then we will assume that it is true; otherwise, false. - // The "1" case is for parsing JSON that assume bits as booleans (i.e. 0 and 1). - String stringValue = getAsString(); - return Boolean.parseBoolean(stringValue) || "1".equals(stringValue); + // Check to see if the value as a String is "true" in any case. + return Boolean.parseBoolean(getAsString()); } } diff --git a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java index 3183b0c2..5c6ec3eb 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java @@ -114,6 +114,9 @@ public final class TypeAdapters { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; + } else if (reader.peek() == JsonToken.STRING) { + // support strings for compatibility with GSON 1.7 + return Boolean.parseBoolean(reader.nextString()); } return reader.nextBoolean(); } diff --git a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java index ac36bc27..821c5cae 100644 --- a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java @@ -38,10 +38,10 @@ public class JsonPrimitiveTest extends TestCase { // Extra support for booleans json = new JsonPrimitive(1); - assertTrue(json.getAsBoolean()); + assertFalse(json.getAsBoolean()); json = new JsonPrimitive("1"); - assertTrue(json.getAsBoolean()); + assertFalse(json.getAsBoolean()); json = new JsonPrimitive("true"); assertTrue(json.getAsBoolean()); diff --git a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java index a9ece86c..e6c3f9a3 100644 --- a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java @@ -21,10 +21,13 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSyntaxException; import com.google.gson.LongSerializationPolicy; +import com.google.gson.reflect.TypeToken; import java.io.Serializable; import java.io.StringReader; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Arrays; +import java.util.List; import junit.framework.TestCase; /** @@ -792,4 +795,10 @@ public class PrimitiveTest extends TestCase { gson.fromJson("12121211243123245845384534687435634558945453489543985435", BigDecimal.class); assertEquals("12121211243123245845384534687435634558945453489543985435", actual.toPlainString()); } + + public void testStringsAsBooleans() { + String json = "['true', 'false', 'TRUE', 'yes', '1']"; + assertEquals(Arrays.asList(true, false, true, false, false), + gson.>fromJson(json, new TypeToken>() {}.getType())); + } }