1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | ========================================================================================================*) starttime:= TIME(); //store the time of the beginning of execution compose:= TRUE; Busy:= TRUE; Aborted:= FALSE; Done:= FALSE; Error:= FALSE; ErrorPosition:= 0; bufferposition:= 1; jsonvarindex:= 1; FOR i:= 1 TO GPL_JSON.MAX_LEVELS DO //clear names array names[i].Name:= ''; names[i].ArrayIndex:= 0; END_FOR //clear previous string SysMemSet( pDest:= JSONString, udiValue:= 0, udiCount:= JSONStringSize ); END_IF IF Abort_TRIG.Q THEN Busy:= FALSE; Aborted:= TRUE; compose:= FALSE; names:= JSONVars^[jsonvarindex].Names; //helper variable for name array property IF (jsonvarindex = 1) THEN objectstr:= CONCAT(objectstr, '{'); //open JSON string with { IF Format THEN objectstr:= CONCAT(objectstr, GPL_JSON.NEWLINE); END_IF ELSE //don't write the value if the current variable is an array, and the value is null FOR namesindex:= JSONVars^[jsonvarindex].ParentVar-1 TO 1 BY -1 DO //start at the second highest variable level and count down //======================open new objects or arrays, or create new name:value pairs====================== IF NOT (VariableArraySize AND (names[1].ArrayIndex > 0) AND (JSONVars^[jsonvarindex].ValueType = JSONVALUETYPE.json_null)) THEN CreateValue(ValueType:= JSONVars^[jsonvarindex].ValueType, Value:= JSONVars^[jsonvarindex].AsString); END_IF ELSE //name:value pair //close objects or arrays if this is the last value in this object or array IF names[namesindex].EndObject THEN //close object at this level CloseObject(); IF names[namesindex].EndArray THEN //close array at this level CloseArray(); END_FOR bufferposition:= bufferposition + DINT_TO_UDINT(LEN(objectstr)); IF (jsonvarindex >= NumberOfVars) THEN //end of vars, close string compose:= FALSE; Busy:= FALSE; Done:= TRUE; ComposeTime:= TIME() - starttime; JSONString^[bufferposition+1]:= ASCII.NULL; //null character to end string //this function block won't take up more than GPL_JSON.MAX_EXECUTION_TIME on one scan //very long strings could take too long to compose in one task cycle, and cause a watchdog exception jsonvarindex:= jsonvarindex + 1; |