SA-MP Forums Archive
toJSON(format_[], {Float,_}:...) HELP - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: toJSON(format_[], {Float,_}:...) HELP (/showthread.php?tid=586766)



toJSON(format_[], {Float,_}:...) HELP - SkittlesAreFalling - 24.08.2015

Код:
stock toJSON(format_[], {Float,_}:...) {
	new string[1024];
	new stringIndex = 0;
	new formatIndex = 0;
	new formatLength = strlen(format_);
	new parameterIndex = 1;
	new parameterCount = numargs();
	new character = '1';
	new argumentIndex = 0;
	new result;
	new Float:resultFloat;
	
	string[stringIndex] = '{'; stringIndex++;

	while(formatIndex < formatLength && parameterIndex < parameterCount && stringIndex < 1000) {
	    // limiting the length to 1000 allows room for the format/number/float. String will check it itself.
		string[stringIndex] = '"'; stringIndex++;

		character = '1';
  		argumentIndex = 0;

		while(character != 0) {
			character = getarg(parameterIndex, argumentIndex);

			if(character != 0) {
			    string[stringIndex] = character; stringIndex++;

			    if(stringIndex + 24 >= 1024) {
			        format(string, 1024, "{null}");
					return string;
				}
			}

			argumentIndex++;
		}

		parameterIndex++;

		string[stringIndex] = '"'; stringIndex++;
		string[stringIndex] = ':'; stringIndex++;

	    switch(format_[formatIndex]) {
	        case '\0': {
		        format(string, 1024, "{null}");
				return string;
			}
			case 'b': {
			    result = getarg(parameterIndex);

				if(result == 0) {
			    	format(string, 1024, "%sfalse", string);
			    	stringIndex += 5;
				}
				else {
			    	format(string, 1024, "%strue", string);
			    	stringIndex += 4;
				}
			}
			case 'i', 'd': {
			    result = getarg(parameterIndex);

			    format(string, 1024, "%s%i", string, result);
		    	stringIndex = strlen(string);
			}
			case 'f': {
			    resultFloat = Float:getarg(parameterIndex);

			    format(string, 1024, "%s%f", string, resultFloat);
		    	stringIndex = strlen(string);
			}
			case 's': {
			    string[stringIndex] = '"'; stringIndex++;

			    character = '1';
			    argumentIndex = 0;

				while(character != 0) {
					character = getarg(parameterIndex, argumentIndex);

					if(character != 0) {
					    string[stringIndex] = character; stringIndex++;

					    if(stringIndex + 3 >= 1024) {
					        format(string, 1024, "{null}");
							return string;
						}
					}

					argumentIndex++;
				}

				string[stringIndex] = '"'; stringIndex++;
			}
			default: {
				continue;
			}
	    }

		formatIndex++;
		parameterIndex++;

		if(formatIndex < formatLength && parameterIndex < parameterCount && stringIndex < 1000) {
			string[stringIndex] = ','; stringIndex++;
		}
	}
	
	string[stringIndex] = '}'; stringIndex++;
	string[stringIndex] = '\0';
	
	printf("%s:%i", string, strlen(string));
	return string;
}
This is my function, it is returning (null) and I do not know why.

Test code:
Код:
	new string[1024];

	format(string, 1024, "%s", toJSON("sibf", "test", "Hello world!", "birthyear", 1994, "HaleyIsCute!", true, "float", 69.69));
	print(string);


Please help me