Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

Re: wwJsonSerializer with Array Support

$
0
0
Re: wwJsonSerializer with Array Support
Web Connection 5.0
Re: wwJsonSerializer with Array Support
Nov. 28, 2012
05:02 pm
3NL10IGDJShow this entire thread in new window
Gratar Image based on email address
From:Rick Strahl
To:Marty Glynn

Web Connection can't deal with PHP style array POST data directly (unless you manually parse the POST data). The better way to post this is to use JSON.stringify().

$.ajax({type: 'POST', url:'./updateUsers.ui', data: JSON.stringify(myArray), content-type: "application/json", success: success,dataType:"text" });

This serializes the data as JSON and sends it to the server. On the server you can then use wwJsonSerializer to deserializer.

FUNCTION GetJson()*** Get JSON from request lcJson = Request.Form()*** Deserialize - only in latest version and possibly *** not until I update to new rev *** (added a new JSON deserializer) loSer = CREATEOBJECT("wwJsonSerializer") loCol = loSer.Deserialize(lcJson) && DeserializeObject(lcJson)FOR lnX = 1 to loCol.Count loItem = loCol.Item(lnX) ...ENDFORENDFUNC

+++ Rick ---



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?

var myArray= [{id:1752, cfirstname:"Marty", clastname:"Glynn"}, {id:1753, cfirstname:"Valrose", clastname:"Lounds"}, {id:1754, cfirstname:"Hung I", clastname:"Lien"}] $.ajax({ type: 'POST', url:'./updateUsers.ui', data:{data:myArray}, success: success, dataType:"html" }); }

aFormVars will return the result below which I don't know what to do with.


data[0][id]: 1752
data[0][cfirstname]: Martyasdf
data[0][clastname]: Glynn
data[1][id]: 1753
data[1][cfirstname]: Valrose
data[1][clastname]: Lounds
data[2][id]: 1754
data[2][cfirstname]: Hung I
data[2][clastname]: Lien


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:"*** Mixed items 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 ?




Rick Strahl
West Wind Technologies


from Maui, Hawaii

Making waves on the Web


Viewing all articles
Browse latest Browse all 10393

Trending Articles