Dynamic variable type?

Just starting out? Need help? Post your questions and find answers here.
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

Dynamic variable type?

Post by Opcode »

My issue is using ExtractJSONStructure to extract JSON data to and array of the same structures for later use.

Code: Select all

  Structure Interaction_Data_Option
    name.s
    type.i
    value
  EndStructure
value can be either integer, double or a string. How could I get value to be able to handle all three data types?
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Dynamic variable type?

Post by mk-soft »

Work not fine with json ...

Code: Select all


Structure Interaction_Data_Option
  name.s
  type.i
  StructureUnion
    iVal.i ; OS 32 or 64 Bit
    lVal.l
    llVal.q
    fltVal.f
    dblVal.d
  EndStructureUnion
  sVal.s
EndStructure

Global var1.Interaction_Data_Option
Global var2.Interaction_Data_Option

var1\type = #PB_Integer
var1\iVal = 1234567890

var2\type = #PB_String
var2\sVal = "Hello World"

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @var1, Interaction_Data_Option)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @var2, Interaction_Data_Option)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Opcode
Enthusiast
Enthusiast
Posts: 138
Joined: Thu Jul 18, 2013 4:58 am

Re: Dynamic variable type?

Post by Opcode »

mk-soft wrote: Sun Jan 01, 2023 7:27 pm Work not fine with json ...

Code: Select all


Structure Interaction_Data_Option
  name.s
  type.i
  StructureUnion
    iVal.i ; OS 32 or 64 Bit
    lVal.l
    llVal.q
    fltVal.f
    dblVal.d
  EndStructureUnion
  sVal.s
EndStructure

Global var1.Interaction_Data_Option
Global var2.Interaction_Data_Option

var1\type = #PB_Integer
var1\iVal = 1234567890

var2\type = #PB_String
var2\sVal = "Hello World"

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @var1, Interaction_Data_Option)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0), @var2, Interaction_Data_Option)
  Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
EndIf
The variable name has to remain the same. I'm receiving the data from a big name company so I must use the variable value in order for ExtractJSONStructure to work.
The extraction is performed recursively if the structure contains further structures, arrays, lists or maps. If the JSON value contains any members that do not have the proper type to match a structure member they will be ignored and the corresponding structure member is left empty.

Any '*' or '$' characters are stripped from the structure member names before comparing them to the JSON object members. So a member key must not include these characters to be properly matched to a structure member.

The comparison of member keys to structure member names is performed case sensitive. If the #JSON data was created or parsed with the #PB_JSON_NoCase flag, the comparison is performed case insensitive.

Code: Select all

Structure Interaction_Data_Option
  name.s
  type.i
  value.s ; Needs to hold integer, double and string (changing it to .d or .i breaks ExtractJSONStructure extracting the data for other results)
EndStructure

Test.Interaction_Data_Option

CreateJSON(0)
JSON = SetJSONObject(JSONValue(0))
SetJSONString(AddJSONMember(JSON, "name"), "demo1")
SetJSONInteger(AddJSONMember(JSON, "type"), 1)
SetJSONString(AddJSONMember(JSON, "value"), "41")

ExtractJSONStructure(JSONValue(0), @Test, Interaction_Data_Option)
FreeJSON(0)

Debug Test\name
Debug Test\type
Debug Test\value

CreateJSON(0)
JSON = SetJSONObject(JSONValue(0))
SetJSONString(AddJSONMember(JSON, "name"), "demo2")
SetJSONInteger(AddJSONMember(JSON, "type"), 2)
SetJSONInteger(AddJSONMember(JSON, "value"), 42)

ExtractJSONStructure(JSONValue(0), @Test, Interaction_Data_Option)
FreeJSON(0)

Debug Test\name
Debug Test\type
Debug Test\value ; Empty string (can't write int in string)

CreateJSON(0)
JSON = SetJSONObject(JSONValue(0))
SetJSONString(AddJSONMember(JSON, "name"), "demo3")
SetJSONInteger(AddJSONMember(JSON, "type"), 3)
SetJSONDouble(AddJSONMember(JSON, "value"), 4.3)

ExtractJSONStructure(JSONValue(0), @Test, Interaction_Data_Option)
FreeJSON(0)

Debug Test\name
Debug Test\type
Debug Test\value ; Empty string (can't write double in string)
Post Reply