Concrete objects with JSON-RPC

jsonJSON-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.

2 Responses to “Concrete objects with JSON-RPC”

  1. Seema B. Says:

    Can you explain the parsing of JSON in java and also how to send and receive using UDP socket?

  2. Robert Sayfullin Says:

    There is number of ways(libraries) you can use for JSON parsing. After some attempts to play with pure JSONObject ( 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’.
    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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: