[Json-rpc-java] Request for comment: suggestion for
direct construction of JSON data structures
Michael Clark
michael at metaparadigm.com
Wed Aug 23 16:12:16 SGT 2006
Ian Sollars wrote:
> Hi all,
>
> I believe I have the latest version of JSON-RPC, in which I see that if I
> bridge an object with this method signature:
>
> public JSONObject doSomething(String a, String b)
>
> ... that the JSONObject is returned to the client as a vanilla Java
> object.
> How difficult would it be to return the result as a direct toString()
> if the
> method return type is JSONObject or JSONArray, i.e. directly
> serialized into
> a JSON data structure?
That sounds quite useful.
> How difficult would this be to change? I've briefly looked at the
> serialization code, but it's pretty complex and I couldn't easily find
> where
> this modification should be made.
Adding two Serializers that just pass through JSONObject and JSONArray
unchanged should do it.
You could try the following patch (compiles and passes existing unit
tests but other than that is untested).
Michael.
--- src/com/metaparadigm/jsonrpc/JSONSerializer.java 6 Mar 2006 12:41:33 -0000 1.15
+++ src/com/metaparadigm/jsonrpc/JSONSerializer.java 23 Aug 2006 07:57:45 -0000
@@ -50,6 +50,8 @@
import com.metaparadigm.jsonrpc.serializer.impl.PrimitiveSerializer;
import com.metaparadigm.jsonrpc.serializer.impl.SetSerializer;
import com.metaparadigm.jsonrpc.serializer.impl.StringSerializer;
+import com.metaparadigm.jsonrpc.serializer.impl.RawJSONObjectSerializer;
+import com.metaparadigm.jsonrpc.serializer.impl.RawJSONArraySerializer;
/**
* This class is the public entry point to the serialization code and provides
@@ -115,6 +117,8 @@
* Register all of the provided standard serializers.
*/
public void registerDefaultSerializers() throws Exception {
+ registerSerializer(new RawJSONArraySerializer());
+ registerSerializer(new RawJSONObjectSerializer());
registerSerializer(new BeanSerializer());
registerSerializer(new ArraySerializer());
registerSerializer(new DictionarySerializer());
--- /dev/null 2006-08-18 15:56:13.282937824 +0800
+++ src/com/metaparadigm/jsonrpc/serializer/impl/RawJSONObjectSerializer.java 2006-08-23 15:50:55.000000000 +0800
@@ -0,0 +1,41 @@
+package com.metaparadigm.jsonrpc.serializer.impl;
+
+import org.json.JSONObject;
+import com.metaparadigm.jsonrpc.serializer.AbstractSerializer;
+import com.metaparadigm.jsonrpc.serializer.MarshallException;
+import com.metaparadigm.jsonrpc.serializer.ObjectMatch;
+import com.metaparadigm.jsonrpc.serializer.SerializerState;
+import com.metaparadigm.jsonrpc.serializer.UnmarshallException;
+
+public class RawJSONObjectSerializer extends AbstractSerializer {
+
+ private final static long serialVersionUID = 2;
+
+ private static Class[] _serializableClasses = new Class[] { JSONObject.class };
+
+ private static Class[] _JSONClasses = new Class[] { JSONObject.class };
+
+ public Class[] getSerializableClasses() {
+ return _serializableClasses;
+ }
+
+ public Class[] getJSONClasses() {
+ return _JSONClasses;
+ }
+
+ public ObjectMatch tryUnmarshall(SerializerState state, Class clazz,
+ Object jso) throws UnmarshallException {
+ return ObjectMatch.OKAY;
+ }
+
+ public Object unmarshall(SerializerState state, Class clazz, Object jso)
+ throws UnmarshallException {
+ return jso;
+ }
+
+ public Object marshall(SerializerState state, Object o)
+ throws MarshallException {
+ return o;
+ }
+
+}
--- /dev/null 2006-08-18 15:56:13.282937824 +0800
+++ src/com/metaparadigm/jsonrpc/serializer/impl/RawJSONArraySerializer.java 2006-08-23 15:55:05.000000000 +0800
@@ -0,0 +1,42 @@
+package com.metaparadigm.jsonrpc.serializer.impl;
+
+import org.json.JSONArray;
+
+import com.metaparadigm.jsonrpc.serializer.AbstractSerializer;
+import com.metaparadigm.jsonrpc.serializer.MarshallException;
+import com.metaparadigm.jsonrpc.serializer.ObjectMatch;
+import com.metaparadigm.jsonrpc.serializer.SerializerState;
+import com.metaparadigm.jsonrpc.serializer.UnmarshallException;
+
+public class RawJSONArraySerializer extends AbstractSerializer {
+
+ private final static long serialVersionUID = 2;
+
+ private static Class[] _serializableClasses = new Class[] { JSONArray.class };
+
+ private static Class[] _JSONClasses = new Class[] { JSONArray.class };
+
+ public Class[] getSerializableClasses() {
+ return _serializableClasses;
+ }
+
+ public Class[] getJSONClasses() {
+ return _JSONClasses;
+ }
+
+ public ObjectMatch tryUnmarshall(SerializerState state, Class clazz,
+ Object jso) throws UnmarshallException {
+ return ObjectMatch.OKAY;
+ }
+
+ public Object unmarshall(SerializerState state, Class clazz, Object jso)
+ throws UnmarshallException {
+ return jso;
+ }
+
+ public Object marshall(SerializerState state, Object o)
+ throws MarshallException {
+ return o;
+ }
+}
+
More information about the Json-rpc-java
mailing list