Topic
  • 6 replies
  • Latest Post - ‏2011-08-08T17:05:17Z by izhd
izhd
izhd
27 Posts

Pinned topic Serialization to/from JSON with JSON/RPC binding

‏2011-07-25T18:11:57Z |
Hello,

I have Service with JSON/RPC binding, and here's the most recent problem I have

I have method in the service defined with input parameter of custom interface, something like

List<String> search(SearchParameter param) where SearchParameter is interface.
Now when I create Dojo object on client that confirms to SearchParameter interface and trying to call the method, it fails with exception java.lang.ClassCastException: com.ibm.json.java.JSONObject incompatible with com.api.SearchParameter.

So is it possible to call method with interface parameter through JSON/RPC binding?

I took a look at most recent release of Tuscany, and it looks like they're using jabsorb and it's serialization there. Are there any plans to upgrade to that version of Tuscany in WAS SCA?

Thanks,
Igor
Updated on 2011-08-08T17:05:17Z at 2011-08-08T17:05:17Z by izhd
  • bhdaniel
    bhdaniel
    7 Posts

    Re: Serialization to/from JSON with JSON/RPC binding

    ‏2011-07-27T17:56:04Z  
    Hi,
    Unfortunately, this is not possible with the current JSON/RPC binding. The JSON databinding has some limitations on the objects it is able to serialize to/from JSON. In this case, it appears that the databinding is only aware of the interface class, so it will not know which class to instantiate to hold the data from the JSON object.

    I am a little surprised that you're getting a ClassCastException, though. I would expect something more along the lines of:
    org.apache.tuscany.sca.databinding.TransformationException: java.lang.RuntimeException: Failed to instantiate com.api.SearchParameter with a no-arg constructor
  • bhdaniel
    bhdaniel
    7 Posts

    Re: Serialization to/from JSON with JSON/RPC binding

    ‏2011-07-27T18:02:11Z  
    Sorry, to answer your other question. There are no current plans to update the existing JSON/RPC binding. The binding in WebSphere is somewhat different from the one in Tuscany as we use dependencies from the Web 2.0 feature pack such as RPCAdapter and JSON4J.
  • izhd
    izhd
    27 Posts

    Re: Serialization to/from JSON with JSON/RPC binding

    ‏2011-07-27T19:31:38Z  
    • bhdaniel
    • ‏2011-07-27T17:56:04Z
    Hi,
    Unfortunately, this is not possible with the current JSON/RPC binding. The JSON databinding has some limitations on the objects it is able to serialize to/from JSON. In this case, it appears that the databinding is only aware of the interface class, so it will not know which class to instantiate to hold the data from the JSON object.

    I am a little surprised that you're getting a ClassCastException, though. I would expect something more along the lines of:
    org.apache.tuscany.sca.databinding.TransformationException: java.lang.RuntimeException: Failed to instantiate com.api.SearchParameter with a no-arg constructor
    Hi bhdaniel,

    That's what I suspected. In jabsorb you can specify hint for the class that should be used for unmarshalling, but I guess it gets even more complicated when we have OSGi implementation.

    It looks like it is not trying to cast incoming object to interface and just calling with JSONObject.

    So I guess the recommendation would be to define interface with JSONObjects?

    Thanks,
    Igor
  • izhd
    izhd
    27 Posts

    Re: Serialization to/from JSON with JSON/RPC binding

    ‏2011-07-27T20:26:03Z  
    • bhdaniel
    • ‏2011-07-27T18:02:11Z
    Sorry, to answer your other question. There are no current plans to update the existing JSON/RPC binding. The binding in WebSphere is somewhat different from the one in Tuscany as we use dependencies from the Web 2.0 feature pack such as RPCAdapter and JSON4J.
    Having said that, is it possible to somehow specify configuration for RPCAdaptor (use aliases, convertors (com.ibm.websphere.rpcadapter.converters.IConverter))?
  • izhd
    izhd
    27 Posts

    Re: Serialization to/from JSON with JSON/RPC binding

    ‏2011-07-29T14:46:44Z  
    • izhd
    • ‏2011-07-27T19:31:38Z
    Hi bhdaniel,

    That's what I suspected. In jabsorb you can specify hint for the class that should be used for unmarshalling, but I guess it gets even more complicated when we have OSGi implementation.

    It looks like it is not trying to cast incoming object to interface and just calling with JSONObject.

    So I guess the recommendation would be to define interface with JSONObjects?

    Thanks,
    Igor
    OK, so I created wrapper that accepts JSONObject in the interface and now getting
    7/29/11 7:30:41:256 PDT 00000031 Collaborator E org.apache.aries.blueprint.proxy.Collaborator invoke invoke
    java.lang.RuntimeException: java.lang.ClassCastException: com.ibm.json.java.JSONObject incompatible with com.ibm.json.java.JSONObject

    I've tried different JSON4J.jar, the one that SCA seems to be using as well (IBM\SDP\runtimes\base_v7\feature_packs\sca\plugins\org.apache.tuscany.sdo.sca.core.model) with the same result. The workaround I found is to cast to Object and do toString to get JSON string, but even that is not good, it gives me {val1=val2, val3=val4} instead of {"val1":"val2", "val3":"val4"} - I need to to do transformation. Any advise on that?
  • izhd
    izhd
    27 Posts

    Re: Serialization to/from JSON with JSON/RPC binding

    ‏2011-08-08T17:05:17Z  
    • izhd
    • ‏2011-07-29T14:46:44Z
    OK, so I created wrapper that accepts JSONObject in the interface and now getting
    7/29/11 7:30:41:256 PDT 00000031 Collaborator E org.apache.aries.blueprint.proxy.Collaborator invoke invoke
    java.lang.RuntimeException: java.lang.ClassCastException: com.ibm.json.java.JSONObject incompatible with com.ibm.json.java.JSONObject

    I've tried different JSON4J.jar, the one that SCA seems to be using as well (IBM\SDP\runtimes\base_v7\feature_packs\sca\plugins\org.apache.tuscany.sdo.sca.core.model) with the same result. The workaround I found is to cast to Object and do toString to get JSON string, but even that is not good, it gives me {val1=val2, val3=val4} instead of {"val1":"val2", "val3":"val4"} - I need to to do transformation. Any advise on that?
    I solved the class-cast issue using reflection.