[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зп-░пп
ґзп-пп
ґзп-пє-╔пії-╗╘╙п╚╛ґ