summaryrefslogtreecommitdiff
path: root/npc/functions/util.txt
diff options
context:
space:
mode:
Diffstat (limited to 'npc/functions/util.txt')
-rw-r--r--npc/functions/util.txt35
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;
+}
+