From 1d73ee5e1b5db3827ea89e7fd098f2d0300bafed Mon Sep 17 00:00:00 2001 From: Jesusaves Date: Fri, 20 Mar 2020 00:01:18 -0300 Subject: Prototype function: json_encode() This is for communication with the API. --- npc/functions/util.txt | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'npc') diff --git a/npc/functions/util.txt b/npc/functions/util.txt index df39c34fa..82611a561 100644 --- a/npc/functions/util.txt +++ b/npc/functions/util.txt @@ -949,4 +949,39 @@ function script numdate { return atoi(.@strdate$); } +// json_encode( {varname, varvalue}, {varname 2, varvalue 2}... ) +// returns string +function script json_encode { + if (getargcount() < 2 || getargcount() % 2 != 0) + return Exception("json_encode arguments must be paired"); + + .@json="{"; + .@tab=true; + + // For arguments + for (.@i=0;.@i < getargcount(); .@i++) { + // Close previous item + if (.@tab) + .@tab=false; + else + .@json+=","; + + // Input variable name + .@json+=getarg(.@i)+": "; + + // Input variable value + if (isstr(getarg(.@i+1))) + .@json+="\""+getarg(.@i+1)+"\""; + else + .@json+=getarg(.@i+1); + + // Advance + .@i++; + } + + // Close the JSON + .@json+="}"; + return .@json; +} + -- cgit v1.2.3-60-g2f50