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

Download this file

7 lines (7 with data), 17.3 kB

Gx_l¬C!0…jüow3ãOèQ
O‚System.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Ò(*======================================================================================================= JSON_TO_STRUCT()VAuthor: Tim Van Meppelen, Pro Electric Inc.(timv@proelectric.com"Date: Nov 2, 2018¨parse JSON string and place the values in a matching local structure of type JSONVARÔ========================================================================================================*)–thisloopstarttime:= TIME();	//store the time of the beginning of this loop	8Execute_TRIG(CLK:= Execute);0Abort_TRIG(CLK:= Abort);,IF Execute_TRIG.Q THENh//must see a rising edge on execute to start parsing†	starttime:= TIME();	//store the time of the beginning of execution	parse:= TRUE;(	bufferposition:= 1; 	Busy:= TRUE;!"	Aborted:= FALSE;"	Done:= FALSE;#	Error:= FALSE;$&	ErrorPosition:= 0;%	obj_level:= 0;&H	FOR i:= 1 TO GPL_JSON.MAX_LEVELS DO'2		NameValue[i].Name:= '';(4		NameValue[i].Value:= '';)>		NameValue[i].IsArray:= FALSE;*<		NameValue[i].ArrayIndex:= 0;+	END_FOR,END_IF-(IF Abort_TRIG.Q THEN.	Busy:= FALSE;/ 	Aborted:= TRUE;0	parse:= FALSE;1z(*====================PARSE JSON STRING====================*)2Š//parse the input string, and start storing values in NameValue array3WHILE parse DO4°	IF (JSONString^[bufferposition] = ASCII.NULL) OR (bufferposition > JSONStringSize) THEN5^	//strings are terminated with a null character6ä	//there might not always be a null character, for example in a network buffer that is stored as an array of bytes7 		parse:= FALSE;8		Done:= TRUE;9		Busy:= FALSE;:B		ParseTime:= TIME() - starttime;;		RETURN;<	END_IF	=J	CASE JSONString^[bufferposition] OF >J	ASCII.DOUBLE_QUOTE:	// " name string?		Name();@		AV	ASCII.OPEN_BRACE:// {, beginning of objectB 		OpenObject();	CL	ASCII.CLOSE_BRACE:// }, end of objectD 		CloseObject();EZ	ASCII.OPEN_BRACKET:	// [, beginning of arrayF		OpenArray();GP	ASCII.CLOSE_BRACKET:	// ], end of arrayH		CloseArray();Id	ASCII.COLON:	// :, separator for name:value pairsJ		Value();KL	ASCII.COMMA:	// , move to next objectL		Comma();M	N’	ASCII.SOH..ASCII.SPACE:	// skip all control characters, including spacesOL		bufferposition:= bufferposition + 1;P
	ELSEQ<	//error, unexpected characterR"		ErrorHandler();S	END_CASETì	(*========================================LOOK FOR NAMES IN LOCAL VARIABLES========================================*)U"	IF findname THENVŒ	//there is at least one valid name:value pair in the NameValue array--check for a match in the Config array, including nested objectsW&		findname:= FALSE;XJ		FOR findloop:= 1 TO NumberOfVars DOYä		(*loop through all configured objects and look for the name that is currently being worked on in the JSON stringZì		Local JSON object should be as closely matched to the expected JSON string as possible, to decrease parsing speed.  [ê		This function will eventually find all objects, but it will start looking after the previous found variable, as it \–		is assumed that that's the most likely position for the next variable*)  ]R			IF (jsonvarindex >= NumberOfVars) THEN^*				jsonvarindex:= 1;_			ELSE`H				jsonvarindex:= jsonvarindex + 1;a			END_IFb|			names:= JSONVars^[jsonvarindex].Names;	//temporary variablecH			FOR nameindex:= 1 TO obj_level DOdÒ			//Match name and array index.  Array index will be 0 if the current name:value is not part of an arrayeÀ				IF (names[nameindex].Name = NameValue[obj_level-(nameindex-1)].Name AND (NameValue[obj_level-(nameindex-1)].ArrayIndex = names[nameindex].ArrayIndex)) THEN	f˜					foundname:= TRUE;	//name in JSON string matches the local variable nameg				ELSEhÊ					foundname:= FALSE;	//name in JSON string does not match the local variable name, go to next namei					EXIT;j				END_IFk			END_FORl(			IF foundname THENm				EXIT;n		END_FORo’		(*====================FOUND NAME, WRITE LOCAL VAR====================*)p&		IF foundname THENq`		//local var name matches with JSON string namer*			foundname:= FALSE;s„			JSONVars^[jsonvarindex].AsString:= NameValue[obj_level].Value;	t		END_IFu	END_IFvˆ	IF TIME() >= (thisloopstarttime + GPL_JSON.MAX_EXECUTION_TIME) THENw¬	//this function block won't take up more than GPL_JSON.MAX_EXECUTION_TIME on one scanxØ	//very long strings or files could take too long to parse in one task cycle, and cause a watchdog exceptionyEND_WHILEzInterface{L{a9ed5b7e-75c5-4651-af16-d2c27e98cb94}|:FUNCTION_BLOCK JSON_TO_STRUCT}VAR_INPUT~|	Execute:			BOOL;		//rising edge triggers parse of JSON stringx	Abort:				BOOL;		//rising edge stops parsing of 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„END_VAR…VAR_OUTPUT†P	Busy:				BOOL;		//busy parsing a string‡Œ	Done:				BOOL;		//successfully parsed the whole string with no errorsˆf	Aborted:			BOOL;		//parsing aborted by Abort input‰ž	Error:				BOOL;		//unexpected character detected, string not completely parsedЬ	ErrorPosition:		UDINT;		//character position where the unexpected character was found‹n	ParseTime:			TIME;		//time it took to parse the stringŒVARr	bufferposition:		UDINT;	//current position in the stringŽÔ	NameValue:			ARRAY[1..GPL_JSON.MAX_LEVELS] OF JSONNAMEVALUE;	//name:value pairs currently being worked on\	obj_level:			UINT;	//number of nested objectsp	name_start:			UDINT;	//index of the beginning of a name‘b	name_stop:			UDINT;	//index of the end of a name’r	value_start:		UDINT;	//index of the beginning of a value“f	value_stop:			UDINT;	//index of the end of a value”r	findname:			BOOL;	//look for name in configuration array•„	foundname:			BOOL;	//found a matching name in configuration array–:	i:					UDINT;	//loop indexes—*	jsonvarindex:		UINT;˜(	levelindex:			UINT;™&	nameindex:			UINT;šÐ	names:				ARRAY[1..GPL_JSON.MAX_LEVELS] OF JSONVARNAME;	//temp variable--can't access an array property›t	thisloopstarttime:	TIME;		//time the current loop startedœV	starttime:			TIME;		//time parsing started.	Execute_TRIG:		R_TRIG;ž,	Abort_TRIG:			R_TRIG;Ÿ$	findloop:			UINT;  	parse:				BOOL;¡"UniqueIdGenerator¢431£POULevel¤L{8e575c5b-1d37-49c6-941b-5c0ec7874787}¥Standard¦ ChildObjectGuids§8System.Collections.ArrayList¨,AddAttributeSubsequent©boolÐÐ-ÐÐ	E
}ÐÐ
­
ÚÐ-ÐÐ
­ÚÐ-ÐÐ
­ÚÐ-ÐÐ
­
ÚÐ-ÐÐ
­ÚÐ-ÐÐ
­ÚÐ-ÐÐ
­%ÚÐ-ÐÐ
­ŠÚÐ-ÐÐ
­ûÚÐ-ÐÐ
­@ÚÐ-ÐÐ
­üÚÐ-ÐÐ
­ýÚÐ-ÐÐ
­/ÚÐ-ÐÐ
­OÚÐ-ÐÐ
­,ÚÐ-ÐÐ
­þÚÐ-ÐÐ
­JÚÐ-ÐÐ
­IÚÐ- ÐÐ
­ÚÐ-!ÐÐ
­ÚÐ-"ÐÐ
­ÚÐ-#ÐÐ
­WÚÐ-$ÐÐ
­XÚÐ-%ÐÐ
­8ÚÐ-&ÐÐ
­:ÚÐ-'ÐÐ
­fÚÐ-(ÐÐ
­gÚÐ-)ÐÐ
­9ÚÐ-*ÐÐ
­ÿÚÐ-+ÐÐ
­AÚÐ-,ÐÐ
­BÚÐ-ÐÐ
­GÚÐ--ÐÐ
­HÚÐ-.ÐÐ
­CÚÐ-/ÐÐ
­DÚÐ-0ÐÐ
­ÚÐ-,ÐÐ
­,ÚÐ-ÐÐ
­-ÚÐ-1ÐÐ
­šÚÐ-2ÐÐ
­¤ÚÐ-3ÐÐ
­¨ÚÐ-4ÐÐ
­©ÚÐ-5ÐÐ
­ªÚÐ-6ÐÐ
­«ÚÐ-7ÐÐ
­¬ÚÐ-8ÐÐ
­­ÚÐ-9ÐÐ
­®ÚÐ-:ÐÐ
­¯ÚÐ-;ÐÐ
­¦ÚÐ-<ÐÐ
­0ÚÐ-ÐÐ
­1ÚÐ-=ÐÐ
­“ÚÐ-ÐÐ
­7ÚÐ->ÐÐ
­VÚÐ-?ÐÐ
­WÚÐ-@ÐÐ
­rÚÐ-AÐÐ
­”ÚÐ-BÐÐ
­^ÚÐ-@ÐÐ
­_ÚÐ-CÐÐ
­jÚÐ-DÐÐ
­kÚÐ-@ÐÐ
­lÚÐ-EÐÐ
­ZÚÐ-FÐÐ
­oÚÐ-@ÐÐ
­pÚÐ-GÐÐ
­rÚÐ-HÐÐ
­sÚÐ-@ÐÐ
­tÚÐ-IÐÐ
­&ÚÐ-JÐÐ
­'ÚÐ-@ÐÐ
­{ÚÐ-KÐÐ
­ ÚÐ-LÐÐ
­$ÚÐ-MÐÐ
­¡ÚÐ-NÐÐ
­©ÚÐ-OÐÐ
­ªÚÐ-ÐÐ
­«ÚÐ-PÐÐ
­.ÚÐ-QÐÐ
­¯ÚÐ-RÐÐ
­°ÚÐ-SÐÐ
­‹ÚÐ-MÐÐ
­²ÚÐ-TÐÐ
­³ÚÐ-UÐÐ
­´ÚÐ-VÐÐ
­µÚÐ-WÐÐ
­¹ÚÐ-XÐÐ
­ÚÐ-YÐÐ
­yÚÐ-ZÐÐ
­zÚÐ-[ÐÐ
­
ÚÐ-\ÐÐ
­ÚÐ-]ÐÐ
­
ÚÐ-^ÐÐ
­ÚÐ-_ÐÐ
­ÚÐ-`ÐÐ
­ºÚÐ-aÐÐ
­»ÚÐ-bÐÐ
­„ÚÐ-cÐÐ
­¿ÚÐ-dÐÐ
­ÂÚÐ-eÐÐ
­ÃÚÐ-fÐÐ
­ÄÚÐ-gÐÐ
­ÅÚÐ-hÐÐ
­ÆÚÐ-iÐÐ
­ÇÚÐ-jÐÐ
­ÈÚÐ-kÐÐ
­ÉÚÐ-lÐÐ
­ÊÚÐ-mÐÐ
­ËÚÐ-aÐÐ
­ÌÚÐ-nÐÐ
­ÍÚÐ-MÐÐ
­ÎÚÐ-oÐÐ
­ÏÚÐ-pÐÐ
­ÐÚÐ-qÐÐ
­ÑÚÐ-rÐÐ
­ÒÚÐ-sÐÐ
­ÓÚÐ-tÐÐ
­ÔÚÐ-uÐÐ
­ÕÚÐ-ÐÐ
­ÚÐ-vÐÐ
­ÚÐ-wÐÐ
­ÖÚÐ-xÐÐ
­×ÚÐ-;ÐÐ
­ÚÐ-uÐÐ
­ÚÐ-yÐÐ
­	ÚÐ-Ðz{Ð	E
*ÐÐ
­ÚÐ-|ÐÐ
­úÚÐ-}ÐÐ
­=ÚÐ-~ÐÐ
­ÚÚÐ-ÐÐ
­ÚÐ-€ÐÐ
­ÛÚÐ-ÐÐ
­ÜÚÐ-‚ÐÐ
­ÝÚÐ-ƒÐÐ
­ÚÐ-„ÐÐ
­ÚÐ-ÐÐ
­FÚÐ-…ÐÐ
­ÚÐ-†ÐÐ
­>ÚÐ-‡ÐÐ
­ÚÐ-ˆÐÐ
­ÚÐ-‰ÐÐ
­MÚÐ-ŠÐÐ
­ÚÐ-‹ÐÐ
­ÞÚÐ-„ÐÐ
­áÚÐ-ÐÐ
­ùÚÐ-ŒÐÐ
­âÚÐ-ÐÐ
­ãÚÐ-ŽÐÐ
­äÚÐ-ÐÐ
­çÚÐ-ÐÐ
­èÚÐ-‘ÐÐ
­éÚÐ-’ÐÐ
­êÚÐ-“ÐÐ
­ëÚÐ-”ÐÐ
­ìÚÐ-•ÐÐ
­íÚÐ-–ÐÐ
­ðÚÐ-—ÐÐ
­ñÚÐ-˜ÐÐ
­òÚÐ-™ÐÐ
­õÚÐ-šÐÐ
­öÚÐ-›ÐÐ
­÷ÚÐ-œÐÐ
­?ÚÐ-ÐÐ
­ÚÐ-žÐÐ
­+ÚÐ-ŸÐÐ
­ÚÐ- ÐÐ
­ÚÐ-„ÐÐ
­ÚÐ-С-¢У¤-¥¦§Ш©­