In order to let JSON-RPC-Java can be transparently unmarshall complex embedded objects, and the JSON-RPC needs to have a mechanism to save type information. We know, JavaScript is a weak type of language, while Java is a strong type, how can I do a corresponding to both? In fact, it is achieved through the direct quantity class of JavaScript.
In the JSON-RPC-Java protocol, the Java class is in JavaScript as follows:
For Java Beans: {"Javaclass": "com.example.mybean", "SomeStringProperty": "foo", "SomeBooleanProperty": True, "SomeintegerProperty": 10}
For list: {"javaclass": "java.util.arrayList", "list": [0, 1, 2, 3, 4]}
For Map: {"Javaclass": "java.util.hashmap", "map": {"foo key": "foo value"}}
For Set: {"javaclass": "java.util.hashset", "set": {"foo key": "foo key"}}
We know that in JavaScript, we can describe the value with directrals, you can create arrays, strings, and objects, etc., which represent constants rather than variable values. Here, the code we have seen above is represented by the number of objects of JavaScript. Object Dip is a list of zero or more pairs of attribute names and relative attribute values, such as Car = {MyCar: "Saturn", GetCar: Cartypes ("Honda"), Special: Sales}, indicates that a Car object is created. It has three attributes mycar, getcar, special, and values are ":" behind the string. With JavaScript direct, we can establish a map between Java objects and JavaScript objects.
Understand that JSON-RPC-Java uses JavaScript direct amount to transfer data between the client and the server, will not feel mysterious about the agreement. Next, I will introduce JSON's JS client:
JavaScript Client JsonRPCCCCLIT creates a transparent way to access all the proxy of all methods of the server, its creation format: var jsonrpc = new jsonrpcclient ("/ WebApp / JSON-RPC /"); you can also add verification information such as: Var JsonRPC = New JSONRPCCLIENT ("/ WebApp / JSON-RPC /", USER, PASS);
The constructor of the JSONRPCCLIENT object first uses a built-in return object All methods that can get the method System.ListMethods Query Server Get the objects that can be accessed, and then these methods are added to the new JSONRPC Client object in the same name as the server side. The client can use it. The client is communicated with the server, there are two ways of synchronization and asynchronous. Synchronization means that the client must wait for the server to respond to the result after the client sends the request, and asynchronously indicates that the client will return to the response after the client is sent. The server will return to the callback function to the callback function. It will be described later), which is a bit like an event processing mechanism. JSON-PRC-Java also supports these two communication processing methods:
For synchronous calls, on the client only: jsonrpc.test.echo ("hello"); // Call the Echo method of the TEST class, output "Hello";
For asynchronous calls, you need to write a callback function (Callback), it is also very simple, just insert the callback function into the method to be called as its first parameter: jsonrpc.test.echo (CB, "Hello" And the write method of the callback function (CB) is as follows, and there are generally two parameters: as the result of Result and possible exception information Exception.
Function CB (Result, Exception) {if (Exception) {alert (Exception.Message);} // do stuff here ...}
Through the above steps, we can use the JSON-RPC-Java protocol to develop their own applications on the basis.
Other knowledge such as exceptions and quotes will be introduced one by one.