PDA

View Full Version : CreateJsonArray



Roel Westhoff [W4]
15-Aug-2018, 05:19 AM
Hello Mike,

How are you doing?

In one of your examples of the workshop 'JSONWorkshop' (EDUC2018) you are using the function CreateJsonArray (Restservice.wo)
The functions is used to create a array of json-objects. The same structure as Dataflex reports RDS uses.

Not sure where i make the mistake but i can't find the function in one of the supplied packages.

Can you help me out?

Tia
Roel

Mike Peat
18-Aug-2018, 04:14 AM
Hi Roel

In my cRestApi_Mixin.pkg I do have a pair of little functions: CreateJsonObject and CreateJsonArray. They just take the pain out of creating a JSON object with "Get Create (RefClass(cJsonObject)) to hoObj" then having to do "Send InitalizeJsonType of hoObj jsonTypeObject" (or jsonTypeArray). Since these two are things you will very frequently want to create (unless you go down the structs route, which I don't do so much these days), I just added them to save typing and reduce code clutter.

They don't do anything magic - just provide JSON containers ready for use:



Function CreateJsonObject Returns Handle
Handle hoObj

Get Create (RefClass(cJsonObject)) to hoObj
Send InitializeJsonType of hoObj jsonTypeObject

Function_Return hoObj
End_Function

Function CreateJsonArray Returns Handle
Handle hoArr

Get Create (RefClass(cJsonObject)) to hoArr
Send InitializeJsonType of hoArr jsonTypeArray

Function_Return hoArr
End_Function


To actually do anything significant with that JSON array (or object), you have to start adding members to it, with AddMember or AddMemberValue in the case of an array, or SetMember or SetMemberValue in the case of an object.

So, to create an array of JSON objects, assuming hoJ1, hoJ2, & hoJ2 are properly initialized JSON objects, you might do:



Get Create (RefClass(cJsonObject)) to hoArr // Or you could use my
Send InitializeJsonType of hoArr jsonTypeArray // CreateJsonArray function here

Send AddMember of hoArr hoJ1
Send AddMember of hoArr hoJ2
Send AddMember of hoArr hoJ3


Mike

Roel Westhoff [W4]
18-Aug-2018, 07:02 AM
Hello Mike,

Thx for the information

The missing piece.

Roel