Page 1 of 1

Dynamic variable type?

Posted: Sun Jan 01, 2023 1:52 pm
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?

Re: Dynamic variable type?

Posted: Sun Jan 01, 2023 7:27 pm
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

Re: Dynamic variable type?

Posted: Sun Jan 01, 2023 10:24 pm
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)