Source: site.view [edit]
Function name: toJSONPC
Arguments: data
Description: Converts a data object into a JSON string for Perfect Cartoon
Page type: webl
Render function:  
Module: siteutil

Page source:

var res = "";

        
var GetValue;

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 = `"` + Wub_ReplaceAll(ToString(value), `"`, `'`) + `"`
   end;
   res
end;

var d2 = [];

var max = 25;
var i = 0;
every d in data.res do
  if i < max then
     d2 = d2 + [ [. imgUrl=d.imgUrl, pageUrl=d.pageUrl, title=d.text, author=d.author .]]
  end;
  i = i + 1
end;

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

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