jsonGetType returns the type of the provided JSON value.
This can be used to determine which function to call when handling unknown values.
jsonGetType can be used to get the type of the elements in a JSON array by changing the idx value.
Input:
o : SYSHANDLE
A handle to a JSON structure.
idx : INT (Default -1)
If idx is -1, the type of o will be returned.
If idx is not -1 and o is a JSON array, the type of o[idx] will be returned.
Returns: INT
7
|
- Null.
|
6
|
- Boolean. Can be read using jsonSetValueBool.
|
5
|
- Floating point number.
|
4
|
- Integer.
|
3
|
- String.
|
2
|
- JSON Array.
|
1
|
- JSON Object.
|
0
|
- Function is not supported.
|
-2
|
- JSON value not found.
|
-3
|
- Array element is not found, idx is outside the valid range
|
-4
|
- idx is not -1 and o is not an array
|
Declaration:
FUNCTION jsonGetType : INT;
VAR_INPUT
o : SYSHANDLE;
idx : INT := -1;
END_VAR;
Example:
FUNCTION INTERFACE dumpJSON;
VAR_INPUT
json : SYSHANDLE;
prefix : STRING;
END_VAR;
END_FUNCTION;
FUNCTION dumpArray;
VAR_INPUT
json : SYSHANDLE;
prefix : STRING;
END_VAR;
VAR
type, i, rc : INT;
tmp : SYSHANDLE;
key : STRING;
txt : STRING;
str : STRING;
d : DINT;
b : BOOL;
f : DOUBLE;
END_VAR;
FOR i := 0 TO jsonArraySize(o:=json) - 1 DO
txt := strFormat(format:=prefix + "[\1]:", v1:=i);
type := jsonGetType(o:=json, idx:= i);
CASE type OF
1,2:
DebugFmt(message:=txt);
rc := jsonGetValue(o:=json, idx:=i, value := tmp);
dumpJSON(json:=tmp, prefix := prefix);
rc := jsonFree(o:=tmp);
3:
jsonGetString(o:=json, idx := i, value :=str);
DebugFmt(message:=txt+":$""+str+"$"");
4:
jsonGetInt(o:=json, idx := i, value :=d);
DebugFmt(message:=txt+":\4", v4:=d);
5:
jsonGetFloat(o:=json, idx := i, value :=f);
DebugFmt(message:=txt+": "+doubleToStr(v:=f));
6:
jsonGetBool(o:=json, idx := i, value :=b);
IF b THEN
DebugFmt(message:=txt+": TRUE");
ELSE
DebugFmt(message:=txt+": FALSE");
END_IF;
7:
DebugFmt(message:=txt+": NULL");
END_CASE;
END_FOR;
END_FUNCTION;
FUNCTION dumpObject;
VAR_INPUT
json : SYSHANDLE;
prefix : STRING;
END_VAR;
VAR
type, i, rc : INT;
tmp : SYSHANDLE;
key : STRING;
txt : STRING;
str : STRING;
d : DINT;
b : BOOL;
f : DOUBLE;
END_VAR;
i := 0;
REPEAT
rc := jsonGetKey(o:=json, idx:= i, key:=key, type:=type);
txt := prefix + "$""+key+"$"";
IF rc = 1 THEN
CASE type OF
1,2:
DebugFmt(message:=txt);
rc := jsonGetValue(o:=json, key:=key, value := tmp);
dumpJSON(json:=tmp, prefix := prefix);
rc := jsonFree(o:=tmp);
3:
jsonGetString(o:=json, key:=key, value :=str);
DebugFmt(message:=txt+":$""+str+"$"");
4:
jsonGetInt(o:=json, key:=key, value :=d);
DebugFmt(message:=txt+":\4", v4:=d);
5:
jsonGetFloat(o:=json, key:=key, value :=f);
DebugFmt(message:=txt+": "+doubleToStr(v:=f));
6:
jsonGetBool(o:=json, key:=key, value :=b);
IF b THEN
DebugFmt(message:=txt+": TRUE");
ELSE
DebugFmt(message:=txt+": FALSE");
END_IF;
7:
DebugFmt(message:=txt+": NULL");
END_CASE;
END_IF;
i := i+1;
UNTIL rc < 0
END_REPEAT;
END_FUNCTION;
FUNCTION IMPLEMENTATION dumpJSON;
VAR
type: INT;
END_VAR;
type := jsonGetType(o:=json, idx:=-1);
IF type = 1 THEN
DebugFmt(message:=prefix+"object{");
dumpObject(json:=json, prefix:=prefix+" ");
DebugFmt(message:=prefix+"}");
END_IF;
IF type = 2 THEN
DebugFmt(message:=prefix+"array[");
dumpArray(json:=json, prefix:=prefix+" ");
DebugFmt(message:=prefix+"]");
END_IF;
END_FUNCTION;
|