]> source.dussan.org Git - vaadin-framework.git/commitdiff
Support null values in legacy messages (#9254) 12/512/1
authorLeif Åstrand <leif@vaadin.com>
Fri, 14 Dec 2012 15:04:26 +0000 (17:04 +0200)
committerLeif Åstrand <leif@vaadin.com>
Fri, 14 Dec 2012 15:04:26 +0000 (17:04 +0200)
Change-Id: I8a7bc1f52ffed7acc2de03c6d17cdff7f640ddf2

server/src/com/vaadin/server/JsonCodec.java
server/tests/src/com/vaadin/server/JSONSerializerTest.java

index 89ef060ef0aed8ce14c22369fa84534a81ba39ae..f7cbe79fdb7eae36887fb272b97042b80f26919b 100644 (file)
@@ -185,6 +185,7 @@ public class JsonCodec implements Serializable {
         registerType(HashMap.class, JsonConstants.VTYPE_MAP);
         registerType(List.class, JsonConstants.VTYPE_LIST);
         registerType(Set.class, JsonConstants.VTYPE_SET);
+        registerType(Void.class, JsonConstants.VTYPE_NULL);
     }
 
     private static void registerType(Class<?> type, String transportType) {
@@ -325,6 +326,9 @@ public class JsonCodec implements Serializable {
 
         if (encodedJsonValue == JSONObject.NULL) {
             return null;
+        } else if (targetType == Void.class) {
+            throw new JSONException(
+                    "Something other than null was encoded for a null type");
         }
 
         // UidlValue
index fc02c137810ae48a1cacffc085c9a69ad0d3ae92..981f74a2a473f52f82beed37f15294aa014628d0 100644 (file)
@@ -16,13 +16,20 @@ package com.vaadin.server;
  * the License.
  */
 import java.lang.reflect.Type;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
 import com.vaadin.server.JsonCodec.BeanProperty;
+import com.vaadin.shared.communication.UidlValue;
 import com.vaadin.shared.ui.splitpanel.AbstractSplitPanelState;
 
 /**
@@ -72,6 +79,26 @@ public class JSONSerializerTest extends TestCase {
         ensureDecodedCorrectly(stateToStringMap, encodedMap, mapType);
     }
 
+    public void testNullLegacyValue() throws JSONException {
+        JSONArray inputArray = new JSONArray(
+                Arrays.asList("n", JSONObject.NULL));
+        UidlValue decodedObject = (UidlValue) JsonCodec.decodeInternalType(
+                UidlValue.class, true, inputArray, null);
+        assertNull(decodedObject.getValue());
+    }
+
+    public void testNullTypeOtherValue() {
+        try {
+            JSONArray inputArray = new JSONArray(Arrays.asList("n", "a"));
+            UidlValue decodedObject = (UidlValue) JsonCodec.decodeInternalType(
+                    UidlValue.class, true, inputArray, null);
+
+            throw new AssertionFailedError("No JSONException thrown");
+        } catch (JSONException e) {
+            // Should throw exception
+        }
+    }
+
     private void ensureDecodedCorrectly(Object original, Object encoded,
             Type type) throws Exception {
         Object serverSideDecoded = JsonCodec.decodeInternalOrCustomType(type,