aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java
diff options
context:
space:
mode:
authorOlli Tietäväinen <ollit@vaadin.com>2019-02-22 10:50:51 +0200
committerSun Zhe <31067185+ZheSun88@users.noreply.github.com>2019-02-22 10:50:51 +0200
commit9d5bdaf89bd173bec7364d591377ca17bb5bb91d (patch)
treecaa481e8d5f29f24be6f11ffd1cac4dfe7f9d887 /uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java
parentc79dca4391bff459f090102447c5502089bf9fc7 (diff)
downloadvaadin-framework-9d5bdaf89bd173bec7364d591377ca17bb5bb91d.tar.gz
vaadin-framework-9d5bdaf89bd173bec7364d591377ca17bb5bb91d.zip
Custom serializers accessors (#10658)
* add accessor methods for CUSTOM_SERIALIZERS in JsonCodec * javadoc * removed removeCustomSerializer method, renamed putCustomSerializer to addCustomSerializer, added sanity checks and JavaDocs * refactored addCustomJsonSerializer to set, added test UI * move enums to be parsed after custom serializers * move adding custom serializer to static block * throw an exception if multiple serializers are registered for class * updated javadocs * changed CustomJSONSerializerTest to a SingleBrowserTest * moved CustomJSONSerializerTest to server/ and it's now not a browser test * removed CustomJSONSerializerTest
Diffstat (limited to 'uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java')
-rw-r--r--uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java63
1 files changed, 63 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java b/uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java
new file mode 100644
index 0000000000..9c8ca554d8
--- /dev/null
+++ b/uitest/src/main/java/com/vaadin/tests/application/CustomJSONSerializer.java
@@ -0,0 +1,63 @@
+package com.vaadin.tests.application;
+
+import java.lang.reflect.Type;
+
+import com.vaadin.server.JsonCodec;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.server.communication.JSONSerializer;
+import com.vaadin.shared.communication.URLReference;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.ConnectorTracker;
+import com.vaadin.ui.LoginForm;
+
+import elemental.json.Json;
+import elemental.json.JsonObject;
+import elemental.json.JsonValue;
+
+public class CustomJSONSerializer extends AbstractTestUI {
+
+ static {
+ JsonCodec.setCustomSerializer(URLReference.class,
+ new JSONSerializer<URLReference>() {
+
+ @Override
+ public URLReference deserialize(Type type,
+ JsonValue jsonValue,
+ ConnectorTracker connectorTracker) {
+ // NOP
+ return null;
+ }
+
+ @Override
+ public JsonValue serialize(URLReference value,
+ ConnectorTracker connectorTracker) {
+ JsonObject result = Json.createObject();
+ String url = value.getURL();
+ // change all test.com urls to vaadin.com
+ if ("http://www.test.com".equals(url)) {
+ url = "http://www.vaadin.com";
+ }
+ result.put("uRL", url);
+ return result;
+ }
+
+ });
+ }
+
+ public static class MyLoginForm extends LoginForm {
+ public void setResource(URLReference ref) {
+ getState().loginResource = ref;
+ }
+ }
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ MyLoginForm loginForm = new MyLoginForm();
+ URLReference url = new URLReference();
+ url.setURL("http://www.test.com");
+ loginForm.setResource(url);
+ addComponent(loginForm);
+ }
+
+}