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.
JSON for VFPIf 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...
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 ClassIn 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")) additivem.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 YouI 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 NextI'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).
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u