From: Leif Åstrand Date: Thu, 28 May 2015 07:47:44 +0000 (+0300) Subject: Encode numeric values as JSON numbers (#18039) X-Git-Tag: 7.5.0.rc1~53 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5dc4c2e9b5944ac5f5f7e327d180b1d244006b2f;p=vaadin-framework.git Encode numeric values as JSON numbers (#18039) Change-Id: Ibb0422ed00de498957e9baf995bb7835b60aafef --- diff --git a/client/src/com/vaadin/client/communication/JsonEncoder.java b/client/src/com/vaadin/client/communication/JsonEncoder.java index fad4ad602a..8d2a447ac5 100644 --- a/client/src/com/vaadin/client/communication/JsonEncoder.java +++ b/client/src/com/vaadin/client/communication/JsonEncoder.java @@ -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) { diff --git a/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java b/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java index a74692b169..6f97cbba05 100644 --- a/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java +++ b/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java @@ -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 index 0000000000..d67b01c71c --- /dev/null +++ b/uitest/src/com/vaadin/tests/serialization/EncodeResultDisplay.java @@ -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 index 0000000000..dc69114f8e --- /dev/null +++ b/uitest/src/com/vaadin/tests/serialization/EncodeResultDisplayTest.java @@ -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 index 0000000000..8cc22de900 --- /dev/null +++ b/uitest/src/com/vaadin/tests/widgetset/client/EncoderResultDisplayConnector.java @@ -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()); + } + +}