Jayrock
From Uweb
Contents |
What is it?
Jayrock is an open source implementation of JSON & JSON-RPC for the Microsoft .NET Framework. Jayrock allows web browser JS clients to be able to call into server-side methods using JSON as the wire format and JSON-RPC as the procedure invocation protocol.
Features
- Uses JSON which is much easier to sift through and parse client-side than SOAP.
- Allows for faster client-server communication than techniques that use XML serialization/deserialization.
- Allows for parsing of JSON text sent by the client into server side obects
- Allows objects that implement the IJsonFormattable interface to be quickly and easily converted into JSON using the JsonWriter class.
- Allows easy identification of JSON RPCs through the JsonRpcMethod attribute.
- Creates JavaScript client proxy representing all the available RPCs on a handler.
Demo
How Tos
Getting Started
First you are going to need to download Jayrock. Unzip it to a directory like C:\Jayrock.
Let's get going by creating a new project. I'll be using VS2005 and a Web Application Project.
Start up Visual Studio and select File->New->Project. Then pick ASP.NET Web Application from the list. Type in a name for your project. I'll be using "JayrockDemo".
Then you are going to need to reference Jayrock from your solution. Right-click "References" in your solution explorer. Click "Add Reference". Then from the "Add Reference" dialog, click the "Browse" tab and navigate to the location of the Jayrock dlls, most likely "C:\Jayrock\bin\Release". Click on "Jayrock.dll" then click the "Add" button. Do the same for "Jayrock.Json.dll".
Writing a handler
Right click on your solution/project file and select Add->New Item ... Pick Generic Handler from the list. Then click "Add".
Delete all the code in your new code-behind file and then enter the following code into your blank file substituting your namespace for "JayrockDemo".
using System.Web.SessionState;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
namespace JayrockDemo
{
public class Handler1 : JsonRpcHandler, IRequiresSessionState
{
}
}
You'll notice that we've extended Jayrock.JsonRpc.Web.JsonHandler with our handler. The JsonRpcHelpAttribute will show up at the top of your proxy script when you request it.
Next we are going to write a method that will return a string array.
[JsonRpcHelp("A method to return a list of cities.")]
[JsonRpcMethod("getCities")]
public string[] GetCities()
{
string[] cities = {"Brenham", "Bryan", "Caldwell", "College Station", "Navasota", "Wellborn"};
return cities;
}
Accessing handler methods via the client-side proxy
When you created your web project, it should have created a file called "Default.aspx". Open up that file and add a <script> tag to your <head> tag to include the proxy script from the server.
<head runat="server">
<script type="text/javascript" src="Handler1.ashx?proxy"></script>
</head>
Note the src attribute, it is important.
Hit F5 to go into Debug mode. If you've got Firefox with Firebug installed, open up the Firebug console and click on the "Script" tab. Select "Handler.aspx?proxy from the list. You should see something like the following pop up in the console:
// This JavaScript was automatically generated by
// Jayrock.JsonRpc.Web.JsonRpcProxyGenerator, Jayrock, Version=0.9.8316.0, Culture=neutral, PublicKeyToken=null
// on Thursday, February 15, 2007 at 3:54:41 PM (Central Standard Time)
// Proxy version 1.0
function Handler1(url)
{
/* A method to return a list of cities. */
this["getCities"] = function(callback)
{
return call("getCities", [ ], callback);
}
...
}
Next you are going to need the json.js javascript file from JSON.org. You can either download it or you can copy it from the JayrockWeb directory from the archive you downloaded.
<head runat="server">
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="Handler1.ashx?proxy"></script>
</head>
Now to create a <script> block to call your RPC method.
<head runat="server">
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="Handler1.ashx?proxy"></script>
<script type="text/javascript">
function handleResponse(response)
{
var select = document.createElement("select");
for(var i = 0; i < response.result.length; i++)
{
select.options[i] = new Option(response.result[i]);
}
document.body.appendChild(select);
}
window.onload = function(){
var handler = new Handler1();
handler.getCities(handleResponse);
}
</script>
</head>
Hit F5 to go into Debug mode again. Now you should have a drop-down list on your page with the names of some area cities in it.
