94ca2d93cd9e8864f86959a9aa8371b7e0151f44
tdreszer
  Wed Nov 30 11:41:55 2011 -0800
Allow minimizing json output with -1 indent level.  While having the apache server DEFLATE will make this change of little value, I thought I would check it in, rather than loose it.
diff --git src/hg/lib/jsHelper.c src/hg/lib/jsHelper.c
index 3c86f68..0f2cf7b 100644
--- src/hg/lib/jsHelper.c
+++ src/hg/lib/jsHelper.c
@@ -563,104 +563,125 @@
 
 void jsonListAddDouble(struct slRef **list, double val)
 {
 char buf[256];
 safef(buf, sizeof(buf), "%.10f", val);
 jsonListAdd(list, (struct jsonElement *) newJsonString(buf));
 }
 
 void jsonListAddBoolean(struct slRef **list, boolean val)
 {
 jsonListAdd(list, (struct jsonElement *) newJsonString(val ? "true" : "false"));
 }
 
 static char *makeIndentBuf(int indentLevel)
 {
+if (indentLevel < 0)
+    return "";
 char *indentBuf;
 indentBuf = needMem(indentLevel + 1);
 memset(indentBuf, '\t', indentLevel);
 indentBuf[indentLevel] = 0;
 return indentBuf;
 }
 
 static void jsonPrintRecurse(struct jsonElement *json, int indentLevel)
 {
+if (indentLevel >= -1) // Note that < -1 will result in no indenting
+    indentLevel++;
+char *tab = "\t";
+char *nl = "\n";
+if (indentLevel < 0)
+    {
+    tab = "";
+    nl = "";
+    }
 char *indentBuf = makeIndentBuf(indentLevel);
 switch (json->type)
     {
     case jsonHash:
         {
         struct jsonHashElement *ele = (struct jsonHashElement *) json;
-        hPrintf("{\n");
+        hPrintf("{%s",nl);
         if(hashNumEntries(ele->hash))
             {
             struct hashEl *el, *list = hashElListHash(ele->hash);
             slSort(&list, hashElCmp);
             for (el = list; el != NULL; el = el->next)
                 {
                 struct jsonElement *val = (struct jsonElement *) el->val;
-                hPrintf("%s\t\"%s\": ", indentBuf, el->name);
-                jsonPrintRecurse(val, indentLevel + 1);
-                hPrintf("%s\n", el->next == NULL ? "" : ",");
+                hPrintf("%s%s\"%s\": ", indentBuf, tab, el->name);
+                jsonPrintRecurse(val, indentLevel);
+                hPrintf("%s%s", el->next == NULL ? "" : ",",nl);
                 }
             hashElFreeList(&list);
             }
         hPrintf("%s}", indentBuf);
         break;
         }
     case jsonList:
         {
         struct jsonListElement *rec = (struct jsonListElement *) json;
         struct slRef *el;
-        hPrintf("[\n");
+        hPrintf("[%s",nl);
         if(rec->list)
             {
             for (el = rec->list; el != NULL; el = el->next)
                 {
                 struct jsonElement *val = (struct jsonElement *) el->val;
-                hPrintf("%s\t", indentBuf);
-                jsonPrintRecurse(val, indentLevel + 1);
-                hPrintf("%s\n", el->next == NULL ? "" : ",");
+                hPrintf("%s%s", indentBuf,tab);
+                jsonPrintRecurse(val, indentLevel);
+                hPrintf("%s%s", el->next == NULL ? "" : ",",nl);
                 }
             }
         hPrintf("%s]", indentBuf);
         break;
         }
     case jsonString:
         {
         hPrintf("%s", ((struct jsonStringElement *) json)->str);
         break;
         }
     default:
         {
         errAbort("jsonPrintRecurse; invalid type: %d", json->type);
         break;
         }
     }
+if (indentLevel >= 0)
 freez(&indentBuf);
 }
 
 void jsonPrint(struct jsonElement *json, char *name, int indentLevel)
 {
-// print out a jsonElement
+// print out a jsonElement, indentLevel -1 means no indenting
 
 char *indentBuf = makeIndentBuf(indentLevel);
 if(name != NULL)
-    hPrintf("// START %s\n%svar %s = ", name, indentBuf, name);
-jsonPrintRecurse(json, indentLevel);
+    {
+    if (indentLevel >= 0 )
+        hPrintf("// START %s\n%s", name, indentBuf);
+    hPrintf("var %s = ", name);
+    }
+jsonPrintRecurse(json, (indentLevel - 1)); // will increment back to indentLevel
 if(name != NULL)
-    hPrintf("%s;\n// END %s\n", indentBuf, name);
+    {
+    hPrintf("%s;\n", indentBuf);
+    if (indentLevel >= 0 )
+        hPrintf("// END %s\n", name);
+    }
+if (indentLevel >= 0)
 freez(&indentBuf);
 }
 
 void jsonErrPrintf(struct dyString *ds, char *format, ...)
 //  Printf a json error to a dyString for communicating with ajax code; format is:
 //  {"error": error message here}
 {
 va_list args;
 va_start(args, format);
 dyStringPrintf(ds, "{\"error\": \"");
 struct dyString *buf = newDyString(1000);
 dyStringVaPrintf(buf, format, args);
 dyStringAppend(ds, javaScriptLiteralEncode(dyStringCannibalize(&buf)));
 dyStringPrintf(ds, "\"}");
 va_end(args);