CLOSE ALL
CLEAR ALL
CLEAR

SET PROCEDURE TO qdfoxJSON
JSONStart()

?"JSON Test #2"
?"Version " + JSON.Version
?
?"THIS TEST SHOWS HOW TO USE ENCODE AND DECODE OBJECTS USING JSONOBJECT CLASS"
?
?"Creating test object..." 

LOCAL o
o = JSONObject()    && Empty object
o.Add("firstName","Victor")    && Normal properties
o.Add("lastName","Espina")
o.Add("Country","Venezuela")

LOCAL ARRAY aLangs[3]
aLangs[1] = "VFP"
aLangs[2] = "C#"
aLangs[3] = "PHP"
o.Add("Languages",@aLangs)    && Array properties

o.addArray("Siblings")   && Collection properties
o.Siblings.Add(JSONObject("{name:'Ender',relation:'Brother'}"))  && Auto-initialized JSON Object
o.Siblings.Add(JSONObject("{name:'Ali',relation:'Brother'}"))
o.Siblings.Add(JSONObject("{name:'Iris',relation:'Sister'}"))

??"Done!"

?"Serializing object..."
LOCAL cJSON
cJSON = o.ToJSON()  
?cJSON

?
?"Deserializing object..."
RELEASE o
o = JSONObject(cJSON)
?"Name"
?"",o.firstName, o.lastName

?"Languages"
#IF VERSION(5) >= 700
 ?"",o.Languages[1]
 ?"",o.Languages[2]
 ?"",o.Languages[3]
#ELSE
 ?"",o.Languages.Item[1]
 ?"",o.Languages.Item[2]
 ?"",o.Languages.Item[3]
#ENDIF
 
?"Siblings"
FOR i = 1 TO o.Siblings.Count
 ?"",o.Siblings.Item(i).Name, o.Siblings.Item(i).Relation
ENDFOR

?
?"Test completed"
?
?"For more information, read initial comments at qdfoxJSON.PRG file"

