better nanoui caching + fixes

Signed-off-by: Mloc <colmohici@gmail.com>
This commit is contained in:
Mloc
2015-05-14 22:35:50 +01:00
parent 1e6921f924
commit cfb51cf198
10 changed files with 61 additions and 89 deletions

View File

@@ -1,7 +1,12 @@
json_writer
var
use_cache = 0
proc
WriteObject(list/L, cached_data = null)
WriteObject(list/L)
if(use_cache && L["__json_cache"])
return L["__json_cache"]
. = "{"
var/i = 1
for(var/k in L)
@@ -9,13 +14,11 @@ json_writer
. += {"\"[k]\":[write(val)]"}
if(i++ < L.len)
. += ","
if(cached_data)
. = copytext(., 1, lentext(.)) + ",\"cached\":[cached_data]}"
. += "}"
write(val)
if(isnum(val))
return num2text(val, 100)
return num2text(val)
else if(isnull(val))
return "null"
else if(istype(val, /list))
@@ -35,27 +38,21 @@ json_writer
. += "]"
write_string(txt)
var/static/list/json_escape = list("\\", "\"", "'", "\n")
var/static/list/json_escape = list("\\" = "\\\\", "\"" = "\\\"", "\n" = "\\n")
for(var/targ in json_escape)
var/start = 1
while(start <= lentext(txt))
var/i = findtext(txt, targ, start)
if(!i)
break
if(targ == "\n")
txt = copytext(txt, 1, i) + "\\n" + copytext(txt, i+2)
start = i + 1 // 1 character added
if(targ == "'")
txt = copytext(txt, 1, i) + "`" + copytext(txt, i+1) // apostrophies fuck shit up...
start = i + 1 // 1 character added
else
txt = copytext(txt, 1, i) + "\\" + copytext(txt, i)
start = i + 2 // 2 characters added
var/lrep = length(json_escape[targ])
txt = copytext(txt, 1, i) + json_escape[targ] + copytext(txt, i + length(targ))
start = i + lrep
return {""[txt]""}
is_associative(list/L)
for(var/key in L)
// if the key is a list that means it's actually an array of lists (stupid Byond...)
if(!isnum(key) && !istype(key, /list))
if(!isnum(key) && !isnull(L[key]) && !istype(key, /list))
return TRUE