January 12, 2011

Replacing JSON Libraries in Delphi REST Servers

If you want to take advantage of jQuery and some of its plug-ins in a Delphi XE DataSnap REST Server application, you should replace the JSON processing library.

I've been working a lot with Delphi XE's DataSnap REST Server projects. I prepared a white paper for Embarcadero, and some introductory videos, and will host a webinar for the company in a couple of weeks (details should be made available soon). I'm also developing a couple of real world projects and building an add-in library (to be called "Delphi Rest Plus" or "Dwarf, Delphi Web Application Rest Framework" or soe other name I still have to figure out...). But that's for another post. Before I get to the actual topic, let me underline I like this new Delphi XE architecture a lot, but it is only a good foundation you have to build more on top.

So, getting to this specific post, if you want to take advantage of jQuery and some of its plug-ins in a Delphi XE DataSnap REST Server application, you should replace the JSON processing library that Embarcadero picked and that is causing lots of incompatibilities. That is the json-min.js file, an open source library by Brenton Fletcher. The specific issues I've had was with the "jQuery validation plug-in 1.7" (http://bassistance.de/jquery-plugins/jquery-plugin-validation/).

As a replacer, I picked the "most official" JavaScript parser for JSON, the public domain json2.js library you can find at http://www.JSON.org/json2.js and see the description at http://www.JSON.org/js.html. Now, of course, I also had to fix the some of the other JavaScript files generated by the DataSnap REST Application wizard in Delphi XE. I don't think I can provide the actual files (since they are proprietary), only list the changes.

In ServerFunctionsExecutor.js you have to change (around line 150) the first line below with the second:

   contentParam = contentParam.toJSONString(); // original
   contentParam = JSON.stringify(contentParam); // new

and (around line 180) you have to fix:

   JSONResultWrapper = responseText.parseJSON(); // original
   JSONResultWrapper = JSON.parse (responseText); // new

The first change (JSON.stringify for toJSONString) has to be done also in the ServerFunctionsInvoker.js file.

That's all for now, but stay tuned for other fixes and improvements to Delphi's REST support... and for the Embarcadero webinar.

PS. Still two more days only for the Extra Discount for Delphi Developer Days Europe: http://www.delphideveloperdays.com/.





 

1 Comments

Replacing JSON Libraries in Delphi REST Servers 

Thank you very much for this fix and your document on
REST Servers in Delphi XE. We intend to rebuild our
application on this framework.
Comment by Joan Dorrepaal on April 9, 07:22


Post Your Comment

Click here for posting your feedback to this blog.

There are currently 0 pending (unapproved) messages.