]> source.dussan.org Git - vaadin-framework.git/commitdiff
Encode numeric values as JSON numbers (#18039)
authorLeif Åstrand <leif@vaadin.com>
Thu, 28 May 2015 07:47:44 +0000 (10:47 +0300)
committerVaadin Code Review <review@vaadin.com>
Wed, 3 Jun 2015 09:27:44 +0000 (09:27 +0000)
Change-Id: Ibb0422ed00de498957e9baf995bb7835b60aafef

client/src/com/vaadin/client/communication/JsonEncoder.java
uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java
uitest/src/com/vaadin/tests/serialization/EncodeResultDisplay.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/serialization/EncodeResultDisplayTest.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/widgetset/client/EncoderResultDisplayConnector.java [new file with mode: 0644]

index fad4ad602aedbec526d05bd9075fcea421eea8b2..8d2a447ac50741b0b10ad9fa42d958d7bb15796f 100644 (file)
@@ -75,8 +75,8 @@ public class JsonEncoder {
             return Json.create((String) value);
         } else if (value instanceof Boolean) {
             return Json.create((Boolean) value);
-        } else if (value instanceof Byte) {
-            return Json.create((Byte) value);
+        } else if (value instanceof Number) {
+            return Json.create(((Number) value).doubleValue());
         } else if (value instanceof Character) {
             return Json.create(String.valueOf(value));
         } else if (value instanceof Object[] && type == null) {
index a74692b169d7bc058fcfae916959c4ee4696d9cb..6f97cbba05d353ea073ddd3f0e926c5c554e5eb8 100644 (file)
@@ -21,7 +21,7 @@ import com.vaadin.ui.VerticalLayout;
 
 public abstract class AbstractTestUIWithLog extends AbstractTestUI {
 
-    protected Log log = new Log(5);
+    protected Log log = new Log(getLogSize());
 
     @Override
     public void init(VaadinRequest request) {
@@ -33,4 +33,8 @@ public abstract class AbstractTestUIWithLog extends AbstractTestUI {
         log.log(message);
     }
 
+    protected int getLogSize() {
+        return 5;
+    }
+
 }
diff --git a/uitest/src/com/vaadin/tests/serialization/EncodeResultDisplay.java b/uitest/src/com/vaadin/tests/serialization/EncodeResultDisplay.java
new file mode 100644 (file)
index 0000000..d67b01c
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.serialization;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.AbstractExtension;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
+import com.vaadin.tests.widgetset.TestingWidgetSet;
+import com.vaadin.tests.widgetset.client.EncoderResultDisplayConnector;
+import com.vaadin.tests.widgetset.client.EncoderResultDisplayConnector.ReportRpc;
+
+@Widgetset(TestingWidgetSet.NAME)
+public class EncodeResultDisplay extends AbstractTestUIWithLog {
+
+    public static class EncoderResultDisplayExtension extends AbstractExtension {
+        public EncoderResultDisplayExtension(EncoderResultDisplayConnector.ReportRpc rpc) {
+            registerRpc(rpc);
+        }
+
+        public void extend(EncodeResultDisplay target) {
+            super.extend(target);
+        }
+    }
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        log.setNumberLogRows(false);
+        new EncoderResultDisplayExtension(new ReportRpc() {
+            @Override
+            public void report(String name, String encodedValue) {
+                log(name + ": " + encodedValue);
+            }
+        }).extend(this);
+    }
+
+    @Override
+    protected int getLogSize() {
+        return 15;
+    }
+}
diff --git a/uitest/src/com/vaadin/tests/serialization/EncodeResultDisplayTest.java b/uitest/src/com/vaadin/tests/serialization/EncodeResultDisplayTest.java
new file mode 100644 (file)
index 0000000..dc69114
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.serialization;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class EncodeResultDisplayTest extends SingleBrowserTest {
+    @Test
+    public void testEncodeResults() {
+        openTestURL();
+
+        int logRow = 0;
+
+        Assert.assertEquals("SimpleTestBean: {\"value\":5}",
+                getLogRow(logRow++));
+        Assert.assertEquals("List: [\"Three\",\"Four\"]", getLogRow(logRow++));
+        Assert.assertEquals("String[]: [\"One\",\"Two\"]", getLogRow(logRow++));
+        Assert.assertEquals("Double: 2.2", getLogRow(logRow++));
+        // PhantomJS likes to add a couple of extra decimals
+        Assert.assertTrue(getLogRow(logRow++).startsWith("Float: 1.1"));
+        Assert.assertEquals("Long: 2147483648", getLogRow(logRow++));
+        Assert.assertEquals("Integer: 3", getLogRow(logRow++));
+        Assert.assertEquals("Byte: 1", getLogRow(logRow++));
+        Assert.assertEquals("Character: \"v\"", getLogRow(logRow++));
+        Assert.assertEquals("String: \"My string\"", getLogRow(logRow++));
+    }
+}
diff --git a/uitest/src/com/vaadin/tests/widgetset/client/EncoderResultDisplayConnector.java b/uitest/src/com/vaadin/tests/widgetset/client/EncoderResultDisplayConnector.java
new file mode 100644 (file)
index 0000000..8cc22de
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.tests.widgetset.client;
+
+import java.util.Arrays;
+import java.util.List;
+
+import com.vaadin.client.ServerConnector;
+import com.vaadin.client.communication.JsonEncoder;
+import com.vaadin.client.extensions.AbstractExtensionConnector;
+import com.vaadin.client.metadata.Type;
+import com.vaadin.client.metadata.TypeData;
+import com.vaadin.shared.communication.ServerRpc;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.tests.serialization.EncodeResultDisplay.EncoderResultDisplayExtension;
+
+import elemental.json.JsonValue;
+
+@Connect(EncoderResultDisplayExtension.class)
+public class EncoderResultDisplayConnector extends AbstractExtensionConnector {
+
+    private ReportRpc reporter;
+
+    public interface ReportRpc extends ServerRpc {
+        public void report(String name, String encodedValue);
+    }
+
+    @Override
+    protected void extend(ServerConnector target) {
+        reporter = getRpcProxy(ReportRpc.class);
+
+        reportEncode("My string");
+        reportEncode(Character.valueOf('v'));
+        reportEncode(Byte.valueOf((byte) 1));
+        reportEncode(Integer.valueOf(3));
+        reportEncode(Long.valueOf(Integer.MAX_VALUE + 1l));
+        reportEncode(Float.valueOf((float) 1.1));
+        reportEncode(Double.valueOf("2.2"));
+
+        reportEncode(new String[] { "One", "Two" });
+        reportEncode(
+                "List",
+                Arrays.asList("Three", "Four"),
+                new Type(List.class.getName(), new Type[] { TypeData
+                        .getType(String.class) }));
+        reportEncode(new SimpleTestBean(5));
+    }
+
+    private void reportEncode(Object value) {
+        Type type = TypeData.getType(value.getClass());
+        reportEncode(value.getClass().getSimpleName(), value, type);
+    }
+
+    private void reportEncode(String name, Object value, Type type) {
+        JsonValue encodedValue = JsonEncoder.encode(value, type,
+                getConnection());
+        reporter.report(name, encodedValue.toJson());
+    }
+
+}