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

wwJsonSerializer with Array Support

$
0
0
wwJsonSerializer with Array Support
Web Connection 5.0
wwJsonSerializer with Array Support
08/03/2012
03:26:43 AM
3KC1FFIND Show this entire thread in new window
Gratar Image based on email address
From:
Rick Strahl
To:
All 
Attachments:
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.Count FOR 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.Count FOR lnX = 1 TO loResult.Count loCust = loResult.Item(lnX) ? loCust.name, loCust.company, loCust.Balance ENDFOR ? "*** 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.Count FOR 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.Count FOR lnX = 1 TO loResult.Count ? loResult.Item(lnX) ENDFOR ? "*** Re-serialize loResult" ? loJson.Serialize(loResult)


? "*** Cursor Serialization" USE wwdemo\TT_CUST SELECT TOP 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.Count FOR lnX = 1 TO loResult.Rows.Count loCust = loResult.Rows.Item(lnX) ? loCust.Company,loCust.Name,loCust.Entered ENDFOR ? "*** Reserialize result object - outputs as an object" lcJson = loJson.Serialize(loResult) ? lcJson ?


Rick Strahl
West Wind Technologies

Making waves on the Web

from Maui, Hawaii


Viewing all articles
Browse latest Browse all 10393

Trending Articles