[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зп-┼пп
ґзп-▀пп
ґчзп-└пп
ґАзп-пп
ґЫзп-▄пп
ґБзп-█пп
ґЦзп-▌пп
ґДзп-▐пп
ґГзп-░пп
ґХзп-▒пп
ґИзп-▓пп
ґЙзп-⌠пп
ґКзп-■пп
ґЛзп-∙пп
ґМзп-√пп
ґПзп-≈пп
ґЯзп-≤пп
ґРзп-≥пп
ґУзп- пп
ґЖзп-⌡пп
ґВзп-°пп
ґ?зп-²пп
ґзп-·пп
ґ+зп-÷пп
ґзп-═пп
ґзп-└пп
ґзп-п║-╒пёє-╔іїп╗╘ґ