17796a0436ab2b1f767d2e571739e2a0c8bdb6ed hiram Mon Jan 28 11:19:07 2019 -0800 compact the hubPublicJsonOutput() as it becomes more generic refs #18869 diff --git src/hg/hubApi/hubApi.c src/hg/hubApi/hubApi.c index b1c2bb2..ddec85b 100644 --- src/hg/hubApi/hubApi.c +++ src/hg/hubApi/hubApi.c @@ -98,104 +98,65 @@ * the back slash */ a = replaceChars(jsonString, "\\", "\\\\"); /* \ -> \\ */ b = replaceChars(a, "\"", "\\\""); /* " -> \" */ freeMem(a); a = replaceChars(b, "/", "\\/"); /* / -> \/ */ freeMem(b); b = replaceChars(a, "\n", "\\\n"); /* \n -> \\n */ freeMem(a); a = replaceChars(b, "\r", "\\\r"); /* \r -> \\r */ freeMem(b); b = replaceChars(a, "\t", "\\\t"); /* \t -> \\t */ return b; } +static void jsonInteger(FILE *f, char *tag, int value) +/* output one json interger: "tag":value appropriately quoted and encoded */ +{ +fprintf(f,"\"%s\":%d",tag, value); +} + +static void jsonString(FILE *f, char *tag, char *value) +/* output one json string: "tag":"value" appropriately quoted and encoded */ +{ +fprintf(f,"\"%s\":",tag); +char *a = jsonEscape(value); +if (isEmpty(a)) + fprintf(f, "%s", "null"); +else + fprintf(f, "\"%s\"", a); +freeMem(a); +} + static void hubPublicJsonOutput(struct hubPublic *el, FILE *f) /* Print out hubPublic element in JSON format. */ { fputc('{',f); -fputc('"',f); -fprintf(f,"hubUrl"); -fputc('"',f); -fputc(':',f); -fputc('"',f); -char *a = jsonEscape(el->hubUrl); -fprintf(f, "%s", a); -freeMem(a); -fputc('"',f); +jsonString(f, "hubUrl", el->hubUrl); fputc(',',f); -fputc('"',f); -fprintf(f,"shortLabel"); -fputc('"',f); -fputc(':',f); -fputc('"',f); -a = jsonEscape(el->shortLabel); -fprintf(f, "%s", a); -freeMem(a); -fputc('"',f); +jsonString(f, "shortLabel", el->shortLabel); fputc(',',f); -fputc('"',f); -fprintf(f,"longLabel"); -fputc('"',f); -fputc(':',f); -fputc('"',f); -a = jsonEscape(el->longLabel); -fprintf(f, "%s", a); -freeMem(a); -fputc('"',f); +jsonString(f, "longLabel", el->longLabel); fputc(',',f); -fputc('"',f); -fprintf(f,"registrationTime"); -fputc('"',f); -fputc(':',f); -fputc('"',f); -a = jsonEscape(el->registrationTime); -fprintf(f, "%s", a); -freeMem(a); -fputc('"',f); +jsonString(f, "registrationTime", el->registrationTime); fputc(',',f); -fputc('"',f); -fprintf(f,"dbCount"); -fputc('"',f); -fputc(':',f); -fprintf(f, "%u", el->dbCount); +jsonInteger(f, "dbCount", el->dbCount); fputc(',',f); -fputc('"',f); -fprintf(f,"dbList"); -fputc('"',f); -fputc(':',f); -fputc('"',f); -a = jsonEscape(el->dbList); -fprintf(f, "%s", a); -freeMem(a); -fputc('"',f); +jsonString(f, "dbList", el->dbList); fputc(',',f); -fputc('"',f); -fprintf(f,"descriptionUrl"); -fputc('"',f); -fputc(':',f); -a = jsonEscape(el->descriptionUrl); -if (isEmpty(a)) - fprintf(f, "%s", "null"); -else - { - fputc('"',f); - fprintf(f, "%s", a); - fputc('"',f); -} -freeMem(a); +jsonString(f, "descriptionUrl", el->descriptionUrl); fputc('}',f); } static int publicHubCmpCase(const void *va, const void *vb) /* Compare two slNames, ignore case. */ { const struct hubPublic *a = *((struct hubPublic **)va); const struct hubPublic *b = *((struct hubPublic **)vb); return strcasecmp(a->shortLabel, b->shortLabel); } static void publicHubSortCase(struct hubPublic **pList) /* Sort slName list, ignore case. */ { slSort(pList, publicHubCmpCase);