File Exchange

image thumbnail

JSONRPC2: a set of classes to encode/decode JSON RPC messages

version 1.0 (47.5 KB) by

Classes to parse and serialise JSON-RPC 2.0 requests/responses. Access message parts via methods.

1 Download


View License

JSON (JavaScript Object Notation) is a highly portable, human-readable and "fat-free" text format to represent complex and hierarchical data. It is as powerful as XML, but less verbose. JSON format is widely used for data-exchange in applications, and is essential for the wild success of Ajax and Web2.0. (JSONlab definition)

Message queues provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them. (Wikipedia definition)

RPC (Remote Procedure Call) allows one computer to execute a procedure (aka method) on another computer. The execution begins when the remote machine receives a message telling it what procedure to execute. RPC packets typically pass a correlation id for matching responses to the original call, response queue name identifying the message queue where responses should be sent, and value which holds one or more messages formatted in a supported protocol (here we are working with messages in the JSONRPC protocol).

JSONRPC is a protocol specifying the format of messages passed via RPC through a message queue. This code implements each of the standard message types. Each type is a separate class with properties to hold the values of the parts allowing them to be accessed (set/get) through method calls and property setters/getters. This implementation supports parameters specified by name or by order.

* JSONRPC2Notification - parts: version, method, params - Notifications specify the method to execute and the parameters to pass to the method. No response is expected of a notification since it does not have an id for associating the notification with a response.

* JSONRPC2Request - parts: version, id, method, params - Requests specify the method to execute and the parameters to pass to the method. A response sending the results which were returned from the method is expected with an id that corresponds to the request's id.

* JSONRPC2Response - parts: version, id, result - Responses specify the results that were returned from the execution of a request's method.

* JSONRPC2Error - parts: version, id, errcode, errmsg - Errors specify any problems that occurred during parsing of the incoming request/notification or execution errors of the method.

NOTE: Version (i.e., JSONRPC protocol version) is always 2.0 for this implementation.

Several pieces of technology need to be in place to work with message queues.

Message Queue Implementation Layer - This handles the sending and receiving messages at the lowest level. No interpretation is placed on the contents of the message. It will define the format of the RPC packets (i.e., correlation_id, reply_to, message). ActiveMQ is an example of a Message Queue implementation. (Not included in this code.)

Message Queue MATLAB Wrapper Layer - ActiveMQ is implemented in JAVA. This layer wraps java methods with MATLAB code allowing the Listerner Implementation Layer access to the Message Queue Implementation Layer (Not included in this code.)

Listener Implementation Layer - This handles listening to a request queue, executing methods, and sending results to the response queue. It uses the Message Queue Wrapper Layer to interact with queues. (Not included in this code.)

JSONRPC Implementation Layer - This handles decoding requests and encoding responses/errors. (THIS CODE)

See jsonrpc2Examples package for working examples.

To run, you must download JSONlab dependency. Tests have additional dependencies. See README.txt in each package for more information.

NOTE: I have code that implements the other layers, but it is not as clean. I can provide that code to you as is upon request.

Comments and Ratings (3)

rasoul rad

I want to send a HTTP request to a web server with URL http://IP address/rpc via RPC protocol and get response. The request must be in JSON format and the server return a JSON formatted response is included some data. I before used webread function but i didn't got correct response. How can i use this file to get data from web server?
I'll be thankful for helps.


The API was modeled as close as possible to that used by Vladimir Dzhuvinov in his Java implementation of JSON-RPC 2.0 Base. For more information on the Java implementation, see his website at...

Also tested on MATLAB R2010a and R2012b

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video