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 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))
...and then to deserialize that JSON string you'd do this:
m.loObjectOut = m.loJSON.Parse(m.lcObjectJSON)
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".
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).