Code covered by the BSD License  

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

Be the first to rate this file! 18 Downloads (last 30 days) File Size: 47.5 KB File ID: #45238
image thumbnail

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

by

 

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

| Watch this File

File Information
Description

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

Technology
----------
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)

Examples
--------
See jsonrpc2Examples package for working examples.

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

Acknowledgements

Matlab X Unit Test Framework, Cprintf Display Formatted Colored Text In The Command Window, and Jso Nlab: A Toolbox To Encode/Decode Json Files In Matlab/Octave inspired this file.

Required Products MATLAB
MATLAB release MATLAB 8.1 (R2013a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
30 Jan 2014 E. L. Rayle

ACKNOWLEDGEMENTS
----------------

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...
http://software.dzhuvinov.com/json-rpc-2.0-base.html

24 Jan 2014 E. L. Rayle

Also tested on MATLAB R2010a and R2012b

Contact us