Files
CHOMPStation2/code/modules/nano/JSON Writer.dm
ccomp5950 89652ae5e3 JSON caching for nanoui
You get to cache data, make the best of it.

NanoUI for PDA now won't shove the manifest through list2json every tick, it will cache the json for manifest and shove it into the cache_data so list2json tacks it onto the end.
2015-01-25 06:22:30 -05:00

62 lines
1.5 KiB
Plaintext

json_writer
proc
WriteObject(list/L, cached_data = null)
. = "{"
var/i = 1
for(var/k in L)
var/val = L[k]
. += {"\"[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)
else if(isnull(val))
return "null"
else if(istype(val, /list))
if(is_associative(val))
return WriteObject(val)
else
return write_array(val)
else
. += write_string("[val]")
write_array(list/L)
. = "\["
for(var/i = 1 to L.len)
. += write(L[i])
if(i < L.len)
. += ","
. += "]"
write_string(txt)
var/static/list/json_escape = list("\\", "\"", "'", "\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
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))
return TRUE