JSON-RPC is one of the most used transport technologies in modern AJAX world. It has many advantages especialy comparing to fatty XHR. However it has .. not drawbacks but rather space for improvement. As you remember the JSON Object is a classic TO and typeless by nature.
Good enough for most cases but sometimes I want to transfer from server to client full-fledged objects that carry internal functionality. Say client-side Customer must have toString() function. So we need a way how to transform typeless JSON transfer objects to class of our domain model defined in JS. The solution is simple. We hint our Java class Customer with name of JS class the same way as we hint JS classes with name of Java class representation (see JSON-RPC manual for more details). On client side we need to cast JSON object to our model class with name taken from jsClass property. To cast? In JS? Yes indeed. myJsonObject.toString = Customer.prototype.toString will do the job if we have few functions. Let say we may define function Customer.decorate(obj) on Customer that will extend given object with all functionality of Customer. Otherwise we may use Prototype library’s metod Object.extend(myJsonObj, Customer) which will extend myJsonObj with all methods that Customer’s methods. So putting all together we have bidiractional Java-JS transfer of typed objects.
January 25, 2007 at 9:39 am |
Can you explain the parsing of JSON in java and also how to send and receive using UDP socket?
January 27, 2007 at 3:17 am |
There is number of ways(libraries) you can use for JSON parsing. After some attempts to play with pure JSONObject (http://www.json.org/java/index.html) I have deliberately chosen JSONRPCBridge for it simplicity, robustness and ease of extending. In this post I have explained some ideas how you can use JSONRPCBridge without actually any involvement of ‘Remoting’. https://aujava.wordpress.com/2006/07/17/xslt-json-rpc-as-alternative-to-jaxb/
With regards to UDP socket I am bit confused why you try to merge two different levels from OSI model. If you explain your use-case it would be easier to suggest you some solution.