Hi Rick,
My education continues. I'm working with nested JSON objects and been reasonably successful following the examples which use some variation on:
- creating collection and empty objects
- looping through data
- creating and populating empty objects which get added to the collection object
- and finally serializing the collection object.
Then I stumbled on the wwDynamic class. If I'm reading the fine documentation correctly, I can use objects based on wwDynamic and then create nested JSON object hierarchies using a much simpler construction method. Am I interpreting this accurately?
TIA
If you're constructing JSON data dynamically, then yes the wwDynamic class is a great way to do it because it combines the assignment of a property and the Propertyname overrides required for getting property names into the right case, all in one pass.
+++ Rick ---
Just to be clear that I'm following, I can add my dynamic JSON into a wwDynamic object. It will automagically add the properly formatted names to the PropertyOverrideNames collection, and when I serialize that object it preserves the case of the property names. Correct?
Do I need to run:
DO wwDynamic
before attempting to create an object based on that class? Or use a SET PROC call?
OK. There must be a magic trick when working with nested JSON objects that I haven't figured out. Here's a code snippet:
m.loCatalog=CREATEOBJECT("wwDynamic")
m.loCatalog.AddProp("groupSettingsID",this.iGroupSettingsID) && this works fine
m.loCatalog.AddProp("timed",this.lTimed) && this works fine
m.lcSourceSessionID=this.getSessionInfo(m.tcSaleno,m.tcLot,[sessionnumber]) && just setting a value for a nested property
m.loCatalog.AddProp("catalogs") && this appears to work fine
m.loCatalog.AddProp("catalogs.sourceSessionID",m.lcSourceSessionID) && this goes kaboom
And this is the error I'm getting:
Message - Incorrect property name.
LineContents - ADDPROPERTY(this.__Reference,lcProperty,lvValue)
What am I missing as far as nesting JSON collections inside outer JSON is concerned?
TIA
So after poking at this for a bit, it looks like it's best suited to JSON objects that do not have nested objects where preserving property name case is required. While I found I could use direct assignment to create nested objects (object.property.value=[xyz]), which is simpler than calling AddProp(), this does not do the PropertyNameOverride magic. And I could not get AddProp() to cooperate with a nested collection of JSON data objects. But feel free to show me the error of my ways. 😃
Thanks.
For nested objects you need to create a nested object assign it and then use AddProp()
on that nested object. IOW, you have to walk the tree.
This works perfectly fine:
DO wwDynamic
DO wwUtils
DO wwJsonSerializer
m.loCatalog=CREATEOBJECT("wwDynamic")
m.loCatalog.AddProp("groupSettingsID","1234") && this works fine
m.loCatalog.AddProp("timed",.T.) && this works fine
m.loCatalog.AddProp("catalogs") && this appears to work fine
*** Separate the Catalogs
m.loCatalog.Catalogs.AddProp("sourceSessionID","4321")
? m.loCatalog.GroupSettingsId && Works
? m.loCatalog.Catalogs.SourceSessionId && Works
loSer = CREATEOBJECT("wwJsonSerializer")
? loSer.Serialize(loCatalog, .T.) && produces properly cased property names
This produces:
{
"catalogs": {
"sourceSessionID": "4321"
},
"groupSettingsID": "1234",
"timed": true
}
The key thing here is that if you create a property without a value, it'll be another wwDynamic object to which you can then add additional properties. If you want anything else you have to explicitly create the value or object (with CreateObject()
)
+++ Rick ---
Thanks. Rick. I thought that might be the strategy.
I've updated the docs and added a child object example using .AddProp()
to clarify:
https://webconnection.west-wind.com/docs/_5ck0pf06k.htm
+++ Rick ---