JSON Serialization Formats

The wwJSONSerializer class serializes FoxPro values in JSON formatted strings. This topic demonstrates how to create output for various types and what the output looks like:

String

Let's start with a simle string serialized into JSON:

loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize("Hello Rick")

returns: "Hello Rick"

Other simple types

including the quotes. Next let's do a DateTime() value which returns a result that is a bit different:

loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize(DateTime())

returns: new Date("Tue, 3 Oct 2006 21:05:34 -1000")

Notice it's an object declaration that gets returned - JSON is essentially a snippet of JavaScript code that JavaScript can execute so here a new date object is created (and yes this is part of the JSON spec). Simple types are easy, so a bool returns true or false, and a number returns 10 or 101.21 without any quotes. These are literal values just as they are in JavaScript.

Objects

You can also serialize objects. Here's an example of a hierarchical object:

loCustomer = CREATEOBJECT("EMPTY") ADDPROPERTY(loCustomer,"Company","West Wind Technologies") ADDPROPERTY(loCustomer,"Name","Rick Strahl") ADDPROPERTY(loCustomer,"Entered",DATETIME()) loAddress = CREATEOBJECT("EMPTY") ADDPROPERTY(loAddress,"Street","32 Kaiea") ADDPROPERTY(loAddress,"City","Paia") ADDPROPERTY(loAddress,"State","HI") ADDPROPERTY(loAddress,"Zip","96779") ADDPROPERTY(loAddress,"Occupants",1) ADDPROPERTY(loCustomer,"Address",loAddress) loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize(loCustomer)

which yields:

{"address":{"city":"Paia","occupants":1,"state":"HI","street":"32 Kaiea","zip":"96779"}, "company":"West Wind Technologies", "entered":new Date("Tue, 3 Oct 2006 21:06:33 -1000"),"name":"Rick Strahl"}

All properties are created inside of JavaScript object notation ({ }) and an inner object is created for the address. As you can see JSON is self-describing in that there's no schema required - the format lets JavaScript know what type you're dealing with.

Note that all properties are rendered in lower case!

FoxPro Cursors or Tables

To serialize a cursor you use a special syntax in the call to Serialize by using "cursor:Alias":

SELECT company,careof, entered ; FROM tt_cust ; into cursor TCustomers ; order by Company loSerializer = CREATEOBJECT("wwJsonSerializer") lcJSON = loSerializer.Serialize("cursor:TCustomers")

The result looks like this:

{"Rows":[{"company":"Avionics Inc. asdasd","careof":"John Doe","entered":new Date("Wed, 16 Mar 2005 00:00:00 -1000")},{"company":"Avis World Headquarters","careof":"Steve Herbin","entered":new Date("Tue, 14 Jun 2005 00:00:00 -1000")},{"company":"BGP Productions","careof":"German Vicencio","entered":new Date("Mon, 4 Jul 2005 00:00:00 -1000")}]}

It basically creates an array of Rows of objects one for each row in the table. Each object then contains properties for each of the fields in the table in lower case.

Why JSON?

You might ask why this format instead of XML? JSON is more light weight than XML, is self-describing and needs no schema to describe the types used in an object. JSON also doesn't need to be parsed on the client. The JavaScript client can simple do:

var ResultValue = evaluate(' + jsonResultString + ');

which is vastly more efficient than any XML parsing would be. In addition JSON is significantly more compact than XML. It's a win win situation.

JSON in Web Connection

JSON is supported in Web Connection with this class and the wwWebAjax control which provides a very simple mechanism for calling FoxPro code on the currently active page in a Web Control Framework page class. For non-Web Control Framework Pages there's also a JSONService class that provides similar functionality with a few lines of code for Process method operation.


  Last Updated: 6/5/2007 | © West Wind Technologies, 2008