Web Connection
Basic JSON object handling question
Gravatar is a globally recognized avatar based on your email address. Basic JSON object handling question
  Richard Kaye
  All
  Dec 30, 2019 @ 10:06am

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

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Rick Strahl
  Richard Kaye
  Dec 30, 2019 @ 12:21pm

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 ---

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Richard Kaye
  Rick Strahl
  Dec 30, 2019 @ 12:48pm

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?

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Richard Kaye
  Rick Strahl
  Dec 30, 2019 @ 12:59pm

Do I need to run:

DO wwDynamic

before attempting to create an object based on that class? Or use a SET PROC call?

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Richard Kaye
  Rick Strahl
  Dec 30, 2019 @ 01:04pm

Ignore that last. Added a SET PROC.

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Richard Kaye
  Rick Strahl
  Dec 30, 2019 @ 03:01pm

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

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Richard Kaye
  Rick Strahl
  Dec 31, 2019 @ 05:37am

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.

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Rick Strahl
  Richard Kaye
  Jan 1, 2020 @ 09:42pm

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 ---

Gravatar is a globally recognized avatar based on your email address. re: Basic JSON object handling question
  Richard Kaye
  Rick Strahl
  Jan 2, 2020 @ 11:04am

Thanks. Rick. I thought that might be the strategy.

© 1996-2024