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 63 64 65 66 67 68 69 70 71 | ========================================================================================================*) 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 (jsonvarindex <> 1) AND (namesindex > 1) AND names[namesindex].NewObject THEN //new object at this level, don't create objects at level 1 IF (names[namesindex].ArrayIndex = 0) THEN //only create an object name if this is not part of an array CreateName(Name:= names[namesindex].Name); END_IF CreateObject(); IF (namesindex=1) THEN //only write a value at level 1 IF (names[namesindex].ArrayIndex > 0) THEN //array, create value only //don't write the value if the current variable is an array, and the value is null CreateValue(ValueType:= JSONVars^[jsonvarindex].ValueType, Value:= JSONVars^[jsonvarindex].AsString); FOR namesindex:= 1 TO JSONVars^[jsonvarindex].ParentVar-1 DO //start at the lowest level and count up //close object at this level, unless it's the last object in the 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]:= 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; |