diff options
author | Jesusaves <cpntb1@ymail.com> | 2020-03-20 00:01:18 -0300 |
---|---|---|
committer | Jesusaves <cpntb1@ymail.com> | 2020-03-20 00:01:18 -0300 |
commit | 1d73ee5e1b5db3827ea89e7fd098f2d0300bafed (patch) | |
tree | 75c2391934f36b148256b72a26a55ac59aa0fd57 | |
parent | 11f0be845afd4722ced1f246ce9c7fc3e6924ea8 (diff) | |
download | serverdata-1d73ee5e1b5db3827ea89e7fd098f2d0300bafed.tar.gz serverdata-1d73ee5e1b5db3827ea89e7fd098f2d0300bafed.tar.bz2 serverdata-1d73ee5e1b5db3827ea89e7fd098f2d0300bafed.tar.xz serverdata-1d73ee5e1b5db3827ea89e7fd098f2d0300bafed.zip |
Prototype function: json_encode()
This is for communication with the API.
-rw-r--r-- | npc/functions/util.txt | 35 |
1 files changed, 35 insertions, 0 deletions
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; +} + |