Source: site.view [edit]
Function name: toJSONPage
Arguments: data
Description: Converts a data object into a JSON string, and returns a page with the data.
Page type: webl
Render function:  
Module: siteutil

Page source:

var res = "";

        
var GetValue;

var Clean = fun(s)
   var s2 = "";
   var err = "";
   every ch in s do
     if (((ch >= '0') and (ch <= 'z')) or (ch == '/') or (Str_IndexOf(ToString(ch), `?=:-_,'!. `) > 0)) then
        s2 = s2 + ch
     else
        err = err + ch
     end
   end;
   return s2
end;

var Body = fun(spc, data)
   var res = "";
   var comma = "";
   if Objectp(data) then
      res = res + "{";
      every field in ToList(data) do
        res = res + comma + `"` + field + `": ` + GetValue(spc + "  ", data[field]);
        comma = ", "
      end;
      res = res + "}"
   elsif Listp(data) then
      res = res + "[";
      every obj in data do
        res = res + comma + GetValue(spc + "  ", obj);
        comma = ", "
      end;
      res = res + "]"
   end;
   res
end;


GetValue = fun(spc, value)
   var res = "";
   if Objectp(value) or Listp(value) then
      res = Body(spc, value)
   elsif Intp(value) then
      res = ToString(value)
   else
      res = `"` + Clean(value) + `"`
   end;
   res
end;

res = res + Body("", data);

res = Wub_ReplaceAll(res, "\n", `\n`);

NewPage(res, "text/plain");