diff options
author | Pekka Hyvönen <pekka@vaadin.com> | 2013-10-11 12:38:53 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-10-11 11:50:04 +0000 |
commit | 2aa2fdcb9fd035789b2cda9a9e6112605d719294 (patch) | |
tree | 1d1d8f2d68017418553986f9e04b1f6f8957791d /server/src | |
parent | 4cb304d8eb63ce0045a2742bd9c945af4f12aa66 (diff) | |
download | vaadin-framework-2aa2fdcb9fd035789b2cda9a9e6112605d719294.tar.gz vaadin-framework-2aa2fdcb9fd035789b2cda9a9e6112605d719294.zip |
Handle ClientMethodInvocation serialization with JSONArray as parameter
(#12532)
Change-Id: I67306d2b9d151614f72455063b0d01423aeed4c1
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/com/vaadin/server/ClientMethodInvocation.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/server/src/com/vaadin/server/ClientMethodInvocation.java b/server/src/com/vaadin/server/ClientMethodInvocation.java index 9c8318b064..3a6a87a53c 100644 --- a/server/src/com/vaadin/server/ClientMethodInvocation.java +++ b/server/src/com/vaadin/server/ClientMethodInvocation.java @@ -16,10 +16,16 @@ package com.vaadin.server; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.io.Serializable; import java.lang.reflect.Method; import java.lang.reflect.Type; +import org.json.JSONArray; +import org.json.JSONException; + /** * Internal class for keeping track of pending server to client method * invocations for a Connector. @@ -80,4 +86,52 @@ public class ClientMethodInvocation implements Serializable, } return Long.signum(getSequenceNumber() - o.getSequenceNumber()); } + + private void writeObject(ObjectOutputStream stream) throws IOException { + // Need to have custom serialization and deserialization because the + // constructor allows parameters of any type with Object[]. Thus, having + // parameters that are not Serializable will lead to + // NotSerializableException when trying to serialize this class. + // An example case of this is in #12532 (JavaScriptCallbackHelper -> + // JSONArray as parameter and not Serializable), for which this + // hac..workaround is implemented. + + // Goes through the parameter types, and apply "custom serialization" to + // the ones that are not Serializable by changing them into something + // that is Serializable. On deserialization (readObject-method below) + // the process should be reversed. + + // Easy way for implementing serialization & deserialization is by + // writing/parsing the object's content as string. + for (int i = 0; i < parameterTypes.length; i++) { + Type type = parameterTypes[i]; + if (type instanceof Class<?>) { + Class<?> clazz = (Class<?>) type; + if (JSONArray.class.isAssignableFrom(clazz)) { + parameters[i] = ((JSONArray) parameters[i]).toString(); + } + } + } + stream.defaultWriteObject(); + } + + private void readObject(ObjectInputStream stream) throws IOException, + ClassNotFoundException { + // Reverses the serialization done in writeObject. Basically just + // parsing the serialized type back to the non-serializable type. + stream.defaultReadObject(); + for (int i = 0; i < parameterTypes.length; i++) { + Type type = parameterTypes[i]; + if (type instanceof Class<?>) { + Class<?> clazz = (Class<?>) type; + if (JSONArray.class.isAssignableFrom(clazz)) { + try { + parameters[i] = new JSONArray(((String) parameters[i])); + } catch (JSONException e) { + throw new IOException(e); + } + } + } + } + } }
\ No newline at end of file |