JavaScript Object Notation – Remote Procedure Call or JSON-RPC defines a handful of data types and commands. The general mechanism consists of two peers establishing a data connection. During the lifetime of a connection, peers may invoke methods provided by the other peer. To invoke a remote method, a request is sent. Unless the request is a notification it must be replied to with a response.
Request
A remote method is invoked by sending a request to a remote service. The request is a single object serialized using JSON.
It has three properties:
Response
When the invocation method completes, the service must reply with a response. The response is a single object serialized using JSON.
It has three properties:
Notification
A notification is a special request which does not have a response. It is a single object serialized using JSON.
The notification has the same properties as the request object with one exception – the id must be null.
JSON-RPC Data Types
There are only simple data types defined in JSON. To overcome this problem in a JSON compatible way a special property for objects is introduced – {"__jsonclass__":["constructor", [param1,...]], "prop1": ...} The object is then instantiated using the constructor, passing in the parameters. Once constructed, the (prop1, ...) properties will be applied.
Examples
--> { "method": "echo", "params": ["Hello JSON-RPC"], "id": 1}
<-- { "result": "Hello JSON-RPC", "error": null, "id": 1}
--> {"method": "postMessage", "params": ["Hello all!"], "id": 99}
<-- {"result": 1, "error": null, "id": 99}
<-- {"method": "handleMessage", "params": ["user1", "we were just talking"], "id": null}
<-- {"method": "handleMessage", "params": ["user3", "sorry, gotta go now, ttyl"], "id": null}
--> {"method": "postMessage", "params": ["I have a question:"], "id": 101}
<-- {"method": "userLeft", "params": ["user3"], "id": null}
<-- {"result": 1, "error": null, "id": 101}