jsonGetType (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

2.10.00


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:

// Dumps JSON structure to device output
FUNCTION INTERFACE dumpJSON;
VAR_INPUT 
   json   : SYSHANDLE;
   prefix : STRING;
END_VAR;
END_FUNCTION;
 
// Dumps array to device output
FUNCTION dumpArray;
VAR_INPUT
   json   : SYSHANDLE;
   prefix : STRING;
END_VAR;
VAR
   typeirc : 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:=jsonidx:= i);      
      CASE type OF 
         1,2:// Object or array
            DebugFmt(message:=txt);
            rc := jsonGetValue(o:=jsonidx:=ivalue := tmp);
            dumpJSON(json:=tmpprefix := prefix);
            rc := jsonFree(o:=tmp);
         3// string
            jsonGetString(o:=jsonidx := ivalue :=str);
            DebugFmt(message:=txt+":$""+str+"$"");
         4// int
            jsonGetInt(o:=jsonidx := ivalue :=d);
            DebugFmt(message:=txt+":\4", v4:=d);
         5// float
            jsonGetFloat(o:=jsonidx := ivalue :=f);
            DebugFmt(message:=txt+": "+doubleToStr(v:=f));
         6// Bool
            jsonGetBool(o:=jsonidx := ivalue :=b);
            IF b THEN
               DebugFmt(message:=txt+": TRUE");
            ELSE
               DebugFmt(message:=txt+": FALSE");
            END_IF;
         7//NULL
            DebugFmt(message:=txt+": NULL");
      END_CASE;
   END_FOR;
END_FUNCTION;
 
// Dumps object to device output
FUNCTION dumpObject;
VAR_INPUT
   json   : SYSHANDLE;
   prefix : STRING;
END_VAR;
VAR
   typeirc : INT;
 
   tmp    : SYSHANDLE;
   key    : STRING;
   txt    : STRING;
   str    : STRING;
   d      : DINT;
   b      : BOOL;
   f      : DOUBLE;
END_VAR;
 
   i := 0;
   REPEAT 
      rc := jsonGetKey(o:=jsonidx:= ikey:=keytype:=type);
      txt := prefix + "$""+key+"$"";
      IF rc = 1 THEN            
         CASE type OF 
         1,2:// Object or array
            DebugFmt(message:=txt);
            rc := jsonGetValue(o:=jsonkey:=keyvalue := tmp);
            dumpJSON(json:=tmpprefix := prefix);
            rc := jsonFree(o:=tmp);
            
         3// string
            jsonGetString(o:=jsonkey:=keyvalue :=str);
            DebugFmt(message:=txt+":$""+str+"$"");
         4// int
            jsonGetInt(o:=jsonkey:=keyvalue :=d);
            DebugFmt(message:=txt+":\4", v4:=d);           
         5//Real
            jsonGetFloat(o:=jsonkey:=keyvalue :=f);
            DebugFmt(message:=txt+": "+doubleToStr(v:=f));   
         6// Bool
            jsonGetBool(o:=jsonkey:=keyvalue :=b);
            IF b THEN
               DebugFmt(message:=txt+": TRUE");
            ELSE
               DebugFmt(message:=txt+": FALSE");
            END_IF;
         7// NULL
            DebugFmt(message:=txt+": NULL");   
         END_CASE;
      END_IF;
      i := i+1;
   UNTIL  rc < 0
   END_REPEAT;
END_FUNCTION;
 
// Implementation of dumpJSON
FUNCTION IMPLEMENTATION dumpJSON;
VAR
   typeINT;
END_VAR;
   
   type := jsonGetType(o:=jsonidx:=-1);
   IF type = 1 THEN // Object
      DebugFmt(message:=prefix+"object{");
      dumpObject(json:=jsonprefix:=prefix+" ");   
      DebugFmt(message:=prefix+"}");
   END_IF;
   
   IF type = 2 THEN // Array
      DebugFmt(message:=prefix+"array[");
      dumpArray(json:=jsonprefix:=prefix+" ");
      DebugFmt(message:=prefix+"]");
   END_IF;
      
END_FUNCTION;