I have Grid Widget (Wijmo.com) that returns an array of objects. What I'd like to do is deserialize the array of objects into a cursor. Is there a better way to post the array?
aFormVars will return the result below which I don't know what to do with.
Hi all,
I spent a bit of time today to add Array and wwCollection de-serialization support into wwJsonParser. The class has always provided solid serialization features, but the lack of array support has been kind of holding this library back a bit.
Although I'm not a huge fan of passing lots of 'complex' data from client to server via JSON, it's nice to allow for arrays, since arrays are quite common for parsing operations.
Anyway, I've added support for array parsing. Arrays are parsed into wwCollection instances and there's support for array elments that are simple types, objects and even other arrays. Arrays can also be of mixed types. Only single dimension arrays are supported.
I've attached an updated version of wwJsonSerializer. Would love to have you guys check it out - try it against existing code and experiment with array deserialization.
Here are a few examples of what now works and that didn't work before:
DO wwJsonSerializer
loJson = CREATEOBJECT("wwJsonSerializer")
? "*** String Array Serialization"
lcJson = '["Value,1","Value2. This \"is\" neat","Value3"]'
? lcJson
loResult =loJson.Deserialize(lcJson)
? loResult.CountFOR lnX = 1 TO loResult.Count
? loResult.Item(lnX)ENDFOR
? "*** Re-serialize String array"
? loJson.Serialize(loResult)
?
? "*** Object array"
lcJson = '[{"name":"Rick","company":"West Wind","Balance":100.05},' + ;'{"name":"Markus","company":"EPS Software","Balance":1100.05},' +;'{"name":"Kevin","company":"OakLeaf","Balance":1300.05},'
loResult =loJson.Deserialize(lcJson)
? loResult.CountFOR lnX = 1 TO loResult.Count
loCust = loResult.Item(lnX)
? loCust.name, loCust.company, loCust.BalanceENDFOR
? "*** re-serialize object array"
? loJson.Serialize(loResult)
?
? "*** Mixed items"
lcJson = '["Value" , {"name":"Rick,"company":"West Wind"} , "Test",10,null,"Test1"]'
?lcJson
loResult =loJson.Deserialize(lcJson)
? loResult.CountFOR lnX = 1 TO loResult.Count
? loResult.Item(lnX)ENDFOR
?
? "*** Nested arrays:"
lcJson = '["Value" , {"name":"Rick","company":"West Wind"} , ["rick","markus"],"Test",10,null,"Test1"]'
?lcJson
loResult =loJson.Deserialize(lcJson)
? loResult.CountFOR lnX = 1 TO loResult.Count
? loResult.Item(lnX)ENDFOR
? "*** Re-serialize loResult"
? loJson.Serialize(loResult)
? "*** Cursor Serialization"
USE wwdemo\TT_CUSTSELECTTOP 3 company,Careof as name, entered ;FROM tt_cust ;ORDER BY company ;
INTO CURSOR TQuery
lcJson = loJson.Serialize("cursor:TQuery")
? lcJson
? "*** De-serialize cursor JSON to object"
loResult =loJson.Deserialize(lcJson)
? loResult
? loResult.Rows.CountFOR lnX = 1 TO loResult.Rows.Count
loCust = loResult.Rows.Item(lnX)
? loCust.Company,loCust.Name,loCust.EnteredENDFOR
? "*** Reserialize result object - outputs as an object"
lcJson = loJson.Serialize(loResult)
? lcJson
?