Browse Source

Encode numeric values as JSON numbers (#18039)

Change-Id: Ibb0422ed00de498957e9baf995bb7835b60aafef
tags/7.5.0.rc1
Leif Åstrand 9 years ago
parent
commit
5dc4c2e9b5

+ 2
- 2
client/src/com/vaadin/client/communication/JsonEncoder.java View 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) {

+ 5
- 1
uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java View 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;
}

}

+ 54
- 0
uitest/src/com/vaadin/tests/serialization/EncodeResultDisplay.java View File

@@ -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;
}
}

+ 43
- 0
uitest/src/com/vaadin/tests/serialization/EncodeResultDisplayTest.java View File

@@ -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++));
}
}

+ 73
- 0
uitest/src/com/vaadin/tests/widgetset/client/EncoderResultDisplayConnector.java View File

@@ -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());
}

}

Loading…
Cancel
Save