# Monday, November 24, 2008

What is JSON?
If you are unfamiliar with JSON, you can dig into the particulars about it here and here. The short explanation is that JSON provides a format for serializing and deserializing variables, arrays, and class instances. For those of you that may not know what serializing and deserializing are, think of serializing as turning a cursor into XML and then think of deserializing as turning XML back into a cursor. Basically the process of turning an instance of MySomething into a string and then turning that string back into an instance of the same MySomething.

Now, besides the obvious advantages of being able to serialize and deserialize VFP values and objects (persisting them on disk or passing them across the wire, just to name a couple), it becomes really useful when you find that you have to interact with Javascript from your Visual FoxPro application. I ran into this while working with Virtual Earth in a webbrowser control (more on that in an upcoming blog entry). I wanted to use JSON as part of my solution because it would allow me to easily move values, instances, and data back and forth between Visual FoxPro and the Javascript that was running inside a page being displayed in the webbrowser control. I could have done without it, but it's a lot more slick when using JSON.

JSON for VFP
If you visited the first "here" in my first paragraph in the section above you may have noted that they have a rather impressive list of languages that provide support, in some manner, for JSON at the bottom of the page. You may have also noted that Visual FoxPro was absent from that list. Now, that doesn't mean that no one has ever done this sort of thing before. In fact, Rick Strahl provides VFP/JSON support through his wwJSONSerializer class and FoxWeb provides similar functionality through it's fwJSON object. And, here in this blog entry, I'm going to provide you with another class that provides JSON support. So, hopefully someday we can get this class and the others listed on the json.org site (anyone know who to call or write to?).

Why Another JSON Class?
So, if Rick and FoxWeb already provide classes, it begs the question, "Why did I decide to write one too?". Well...

  • When I started working on this I didn't know that Rick even provided such a class in web connect. As for FoxWeb, I've never used it, so I don't know what their object does. Heck, for all I know there are other classes out there besides these two. If you know of any others, please feel free to comment on this blog entry with a link.
  • My JSON class provides additional support for some things when compared to the functionality of wwJSONSerializer:
    • Multi-dimensional array support
    • Replacer support on stringify
    • Reviver support on parse
    • Beautification/indetation support
    • Performance enhancements via FLL
    • A few other things, but those are the main ones
  • The implementation of my JSON class is patterned after json2.js (by Douglas Crockford), which means:
    • The main methods are Stringify() and Parse()
    • It supports datetimes in ISO8601 format (other formats available)
    • Provides the same default escaping of characters (other modes available).
  • Because I wanted to try my hand at this?

There's no better way that I know of to learn something than to write a wrapper or even a class that directly interacts with another library or API. So, I wrote a top-notch JSON class library. I am using it in a production project at this end (my customer was kind enough to give me permission to share it) and I am blogging about it in the hopes some of you may find it useful.

How to Use the JSON Class
In the class, I have provided lots of comments in the code to help document this thing. Also, provided in the download is a jsontests.prg that provides some sample use code you can look at to get started. For the sake of blog brevity... in order to serialize a Visual FoxPro object to JSON you'd do the following:

m.loJSON = CREATEOBJECT("JSON")
SET CLASSLIB TO (LOCFILE("JSON.VCX", "VCX", "Locate JSON Class Library")) additive
m.loObjectIn = CREATEOBJECT("Custom"
m.lcObjectJSON = m.loJSON.Stringify(m.loObjectIn, null, CHR(9))
?m.lcObjectJSON

...and then to deserialize that JSON string you'd do this:

m.loObjectOut = m.loJSON.Parse(m.lcObjectJSON)
?m.loObjectOut.Class

As I say, the code is heavily commented (so check it out) and I'll be doing a blog entry in the near future regarding a Virtual Earth class library I've written for VFP that shows some real world uses for JSON within VFP.

Special Thank You
I sincerely appreciate the efforts of those of you that have already been providing valuable feedback, such as Steven Black and Marc Lyon. Also, Rick Strahl, who provided help and sage advice to me via Twitter as I worked through the particulars of this class. It's always nice to have someone advising you who has already "been there, done that".

What's Next
I'm hopeful that those of you that download this class will take the time to provide me feedback and any suggestions you may have for further improving it.

Until next time... Visual FoxPro Rocks!

Download the JSON class library (approx. 52 KB)

The download contains the JSON class in both VCX and PRG format. Use whichever one you are more comfortable with (they are the same).

Monday, November 24, 2008 12:38:28 PM (GMT Standard Time, UTC+00:00)  #    Comments [9]

 

Archive

<July 2014>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789