[r2]: / trunk / Functions / STRUCT_TO_JSON / svnobj  Maximize  Restore  History

Download this file

6 lines (6 with data), 18.0 kB

Gx_lCF!0…jüoº4xéla\SÄ8System.StringL{6f9dac99-8de1-4efc-8465-68ac443b7d08}SpecialFuncL{0db3d7bb-cde0-4416-9a7b-ce49a0124323}NoneImplementationL{3b83b776-fb25-43b8-99f2-3c507c9143fc}TextDocument	L{f3878285-8e4f-490b-bb1b-9acbb7eb04db}
TextLinesL{a5de0b0b-1cb5-4913-ac21-9d70293ec00d}Id
longTagTextÒ(*======================================================================================================= STRUCT_TO_JSON()VAuthor: Tim Van Meppelen, Pro Electric Inc.(timv@proelectric.com"Date: Nov 2, 2018lcreate a JSON string from a structure of JSONVAR typesÔ========================================================================================================*)”thisloopstarttime:= TIME();	//store the time of the beginning of this loop8Execute_TRIG(CLK:= Execute);0Abort_TRIG(CLK:= Abort);,IF Execute_TRIG.Q THENl//must see a rising edge on execute to start composing†	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;	&H	FOR i:= 1 TO GPL_JSON.MAX_LEVELS DO'(	//clear names array(*		names[i].Name:= '';)4		names[i].ArrayIndex:= 0;*	END_FOR+0	//clear previous string,	SysMemSet(-,		pDest:= JSONString, . 		udiValue:= 0, /6		udiCount:= JSONStringSize0	);1END_IF2(IF Abort_TRIG.Q THEN3	Busy:= FALSE;4 	Aborted:= TRUE;5"	compose:= FALSE;6´WHILE compose AND (jsonvarindex <= NumberOfVars) AND (bufferposition < JSONStringSize) DO 7D//loop through all local variables8`	objectstr:= '';	//clear previous object string	9¢	names:= JSONVars^[jsonvarindex].Names;	//helper variable for name array property:	;6	IF (jsonvarindex = 1) THEN<~		objectstr:= CONCAT(objectstr, '{');	//open JSON string with {= 		IF Format THEN>f			objectstr:= CONCAT(objectstr, GPL_JSON.NEWLINE);?		END_IF@
	ELSEA‚		IF NOT (VariableArraySize AND (names[1].ArrayIndex > 0) AND (JSONVars^[jsonvarindex].ValueType = JSONVALUETYPE.json_null)) THENB¨		//don't write the value if the current variable is an array, and the value is nullCˆ			objectstr:= CONCAT(objectstr, ',');	//comma in between each valueD`			IF Format AND (names[1].ArrayIndex <= 1) THENE`			//new line after each comma, except in arraysFj				objectstr:= CONCAT(objectstr, GPL_JSON.NEWLINE);	G			END_IF	H	END_IFIˆ	FOR namesindex:= JSONVars^[jsonvarindex].ParentVar-1 TO 1 BY -1 DO Jx	//start at the second highest variable level and count downK˜	//open new objects or arrays, and write a single name:value pair at level 1LÔ		//======================open new objects or arrays, or create new name:value pairs======================M˜		IF names[namesindex].NewArray AND (names[namesindex].ArrayIndex <= 1) THENN6		//new array at this levelOZ			CreateName(Name:= names[namesindex].Name);P$			CreateArray();	Q		END_IF	Rt		IF (namesindex > 1) AND names[namesindex].NewObject THENSz		//new object at this level, don't create objects at level 1TZ			IF (names[namesindex].ArrayIndex = 0) THENU~			//only create an object name if this is not part of an arrayV\				CreateName(Name:= names[namesindex].Name);W			END_IFX&			CreateObject();	Y		Z0		IF (namesindex=1) THEN[B		//only write a value at level 1\Z			IF (names[namesindex].ArrayIndex > 0) THEN]:			//array, create value only^†				IF NOT (VariableArraySize AND (names[1].ArrayIndex > 0) AND (JSONVars^[jsonvarindex].ValueType = JSONVALUETYPE.json_null)) THEN_¬				//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);					a				END_IFb			ELSEc(			//name:value pairdÒ				CreateValue(ValueType:= JSONVars^[jsonvarindex].ValueType, Value:= JSONVars^[jsonvarindex].AsString);e|	FOR namesindex:= 1 TO JSONVars^[jsonvarindex].ParentVar-1 DO fR	//start at the lowest level and count upg˜	//close objects or arrays if this is the last value in this object or arrayhJ		IF names[namesindex].EndObject THENi<		//close object at this levelj"			CloseObject();kJ		IF names[namesindex].EndArray THEN	l:		//close array at this levelm"			CloseArray();	n	END_FOR	oˆ	//concatenate the temporary object string to the JSON string outputp	StrConcatA(q8		pstFrom:= ADR(objectstr), rX		pstTo:= ADR(JSONString^[bufferposition]), sX		iBufferSize:= UDINT_TO_INT(JSONStringSize)t	);	u‚	bufferposition:= bufferposition + DINT_TO_UDINT(LEN(objectstr));vN	IF (jsonvarindex >= NumberOfVars) THENw8	//end of vars, close stringx$		compose:= FALSE;y		Busy:= FALSE;z 		Done:= TRUE;		{F		ComposeTime:= TIME() - starttime;|œ		JSONString^[bufferposition]:= ASCII.CLOSE_BRACE;;	//close JSON object with }}˜		JSONString^[bufferposition+1]:= ASCII.NULL;	//null character to end string~ˆ	IF TIME() >= (thisloopstarttime + GPL_JSON.MAX_EXECUTION_TIME) THEN¬	//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		RETURN;‚D	jsonvarindex:= jsonvarindex + 1;	ƒEND_WHILE„Interface…L{a9ed5b7e-75c5-4651-af16-d2c27e98cb94}†:FUNCTION_BLOCK STRUCT_TO_JSON‡VAR_INPUTˆ€	Execute:			BOOL;		//rising edge triggers compose of JSON string‰z	Abort:				BOOL;		//rising edge stops composing a JSON stringŠú	{attribute 'displaymode':='hex'}JSONString:		POINTER TO ARRAY[1..GPL_JSON.MAX_JSON_STRING] OF BYTE;	//pointer to JSON string‹T	JSONStringSize:		UDINT;		//size of stringŒè	JSONVars:			POINTER TO ARRAY[1..GPL_JSON.MAX_VARS] OF JSONVAR;	//pointer to local structure made up of type JSONVAR˜	NumberOfVars:		UINT;		//number of variables in the local structure or arrayŽÎ	VariableArraySize:	BOOL;		//don't write array values which are null, resulting in variable array sizes”	Format:				BOOL;		//add white space and indenting to the resulting stringEND_VAR‘VAR_OUTPUT’T	Busy:				BOOL;		//busy composing a string“	Done:				BOOL;		//successfully composed the whole string with no errors”f	Aborted:			BOOL;		//compose aborted by Abort input•¢	Error:				BOOL;		//unexpected character detected, string not completely composed––	ErrorPosition:		UDINT;		//index of local object at which composing stopped—t	ComposeTime:		TIME;		//time it took to compose the string˜VAR™r	bufferposition:		UDINT;	//current position in the stringš,	jsonvarindex:		UDINT;›$	compose:			BOOL;	œ~	objectstr:			STRING(255);	//string created by current variableÆ	names:				ARRAY[1..GPL_JSON.MAX_LEVELS] OF JSONVARNAME;	//helper variable for names array propertyž&	namesindex:			INT;Ÿ	i:					UINT; .	Execute_TRIG:		R_TRIG;¡,	Abort_TRIG:			R_TRIG;¢t	thisloopstarttime:	TIME;		//time the current loop started£Z	starttime:			TIME;		//time composing started¤"UniqueIdGenerator¥1193¦POULevel§L{8e575c5b-1d37-49c6-941b-5c0ec7874787}¨Standard© ChildObjectGuidsª8System.Collections.ArrayList«,AddAttributeSubsequent¬boolÐÐ-ÐÐ	E
ÐÐ
­%ÚÐ-ÐÐ
­&ÚÐ-ÐÐ
­'ÚÐ-ÐÐ
­(ÚÐ-ÐÐ
­)ÚÐ-ÐÐ
­*ÚÐ-ÐÐ
­2ÚÐ-ÐÐ
­ÝÚÐ-ÐÐ
­ÞÚÐ-ÐÐ
­ØÚÐ-ÐÐ
­ÙÚÐ-ÐÐ
­ÚÚÐ-ÐÐ
­ÛÚÐ-ÐÐ
­ÜÚÐ-ÐÐ
­ÝÚÐ-ÐÐ
­ÞÚÐ-ÐÐ
­ßÚÐ-ÐÐ
­áÚÐ-ÐÐ
­âÚÐ- ÐÐ
­ãÚÐ-!ÐÐ
­äÚÐ-"ÐÐ
­ÚÐ-#ÐÐ
­ÚÐ-$ÐÐ
­ÚÐ-%ÐÐ
­KÚÐ-&ÐÐ
­@ÚÐ-'ÐÐ
­ØÚÐ-(ÐÐ
­MÚÐ-)ÐÐ
­—ÚÐ-*ÐÐ
­’ÚÐ-+ÐÐ
­“ÚÐ-,ÐÐ
­”ÚÐ--ÐÐ
­•ÚÐ-.ÐÐ
­–ÚÐ-/ÐÐ
­5ÚÐ-0ÐÐ
­íÚÐ-1ÐÐ
­îÚÐ-ÐÐ
­ïÚÐ-2ÐÐ
­ðÚÐ-3ÐÐ
­ñÚÐ-4ÐÐ
­òÚÐ-5ÐÐ
­óÚÐ-1ÐÐ
­3ÚÐ-ÐÐ
­5ÚÐ-ÐÐ
­ûÚÐ-ÐÐ
­6ÚÐ-6ÐÐ
­ÛÚÐ-7ÐÐ
­æÚÐ-8ÐÐ
­öÚÐ-9ÐÐ
­ÚÐ-:ÐÐ
­ÚÐ-;ÐÐ
­rÚÐ-<ÐÐ
­sÚÐ-=ÐÐ
­tÚÐ->ÐÐ
­ÚÐ-?ÐÐ
­™ÚÐ-@ÐÐ
­©ÚÐ-AÐÐ
­ŸÚÐ-BÐÐ
­ ÚÐ-CÐÐ
­¡ÚÐ-DÐÐ
­¢ÚÐ-EÐÐ
­£ÚÐ-FÐÐ
­›ÚÐ-GÐÐ
­kÚÐ-?ÐÐ
­ÚÐ-ÐÐ
­4ÚÐ-HÐÐ
­IÚÐ-ÐÐ
­”ÚÐ-IÐÐ
­"ÚÐ-JÐÐ
­LÚÐ-KÐÐ
­OÚÐ-LÐÐ
­TÚÐ-MÐÐ
­]ÚÐ-NÐÐ
­_ÚÐ-OÐÐ
­VÚÐ-PÐÐ
­bÚÐ-QÐÐ
­ÚÐ-ÐÐ
­dÚÐ-RÐÐ
­eÚÐ-SÐÐ
­gÚÐ-TÐÐ
­aÚÐ-UÐÐ
­fÚÐ-VÐÐ
­`ÚÐ-WÐÐ
­
ÚÐ-XÐÐ
­ÚÐ-?ÐÐ
­qÚÐ-YÐÐ
­äÚÐ-ZÐÐ
­ÚÐ-[ÐÐ
­ÀÚÐ-\ÐÐ
­˜ÚÐ-]ÐÐ
­¤ÚÐ-^ÐÐ
­¥ÚÐ-_ÐÐ
­¦ÚÐ-`ÐÐ
­ÆÚÐ-aÐÐ
­ÁÚÐ-bÐÐ
­³ÚÐ-cÐÐ
­´ÚÐ-VÐÐ
­±ÚÐ-dÐÐ
­|ÚÐ-WÐÐ
­àÚÐ-?ÐÐ
­EÚÐ-*ÐÐ
­FÚÐ-:ÐÐ
­GÚÐ-eÐÐ
­HÚÐ-fÐÐ
­RÚÐ-gÐÐ
­;ÚÐ-hÐÐ
­<ÚÐ-iÐÐ
­=ÚÐ-jÐÐ
­>ÚÐ-?ÐÐ
­?ÚÐ-ÐÐ
­@ÚÐ-kÐÐ
­AÚÐ-lÐÐ
­BÚÐ-mÐÐ
­CÚÐ-QÐÐ
­DÚÐ-nÐÐ
­¸ÚÐ-:ÐÐ
­¹ÚÐ-oÐÐ
­ºÚÐ-pÐÐ
­»ÚÐ-qÐÐ
­‘ÚÐ-rÐÐ
­¼ÚÐ-sÐÐ
­9ÚÐ-tÐÐ
­OÚÐ-:ÐÐ
­@ÚÐ-uÐÐ
­ýÚÐ-ÐÐ
­ˆÚÐ-vÐÐ
­ŠÚÐ-wÐÐ
­‹ÚÐ-xÐÐ
­ŒÚÐ-yÐÐ
­‰ÚÐ-zÐÐ
­?ÚÐ-{ÐÐ
­†ÚÐ-|ÐÐ
­‡ÚÐ-}ÐÐ
­ÚÐ-HÐÐ
­þÚÐ-:ÐÐ
­ÿÚÐ-~ÐÐ
­ÚÐ-ÐÐ
­ÚÐ-€ÐÐ
­ÚÐ-ÐÐ
­ÚÐ-HÐÐ
­ÚÐ-:ÐÐ
­ÚÐ-‚ÐÐ
­{ÚÐ-ƒÐÐ
­	ÚÐ-Є…Ð	E
%ÐÐ
­ÆÚÐ-†ÐÐ
­ÈÚÐ-‡ÐÐ
­ÉÚÐ-ˆÐÐ
­ÊÚÐ-‰ÐÐ
­+ÚÐ-ŠÐÐ
­ËÚÐ-‹ÐÐ
­ÌÚÐ-ŒÐÐ
­ÚÐ-ÐÐ
­ùÚÐ-ŽÐÐ
­ÍÚÐ-ÐÐ
­ÇÚÐ-ÐÐ
­ÚÐ-ÐÐ
­ÎÚÐ-‘ÐÐ
­ÏÚÐ-’ÐÐ
­ÐÚÐ-“ÐÐ
­ÑÚÐ-”ÐÐ
­ÒÚÐ-•ÐÐ
­<ÚÐ-–ÐÐ
­ÓÚÐ-—ÐÐ
­ÕÚÐ-ÐÐ
­ÚÐ-ÐÐ
­ÚÐ-˜ÐÐ
­iÚÐ-™ÐÐ
­ÚÐ-šÐÐ
­hÚÐ-›ÐÐ
­ÚÐ-œÐÐ
­ÚÐ-ÐÐ
­nÚÐ-žÐÐ
­ÚÐ-ŸÐÐ
­ÖÚÐ-:ÐÐ
­×ÚÐ- ÐÐ
­ùÚÐ-¡ÐÐ
­úÚÐ-¢ÐÐ
­üÚÐ-£ÐÐ
­nÚÐ-ÐÐ
­ÚÐ-ÐÐ
­ÚÐ-Ф-¥Ц§-¨©ªЫ¬­