f2b6a945eb6990470e8a4435b980fa22aa567b11 angie Fri Sep 11 13:28:00 2015 -0700 When a NULL tag ("var") was passed to jsonListStart, it wasn't printing the preceding comma if one was needed. To make NULL tag universally supported by everything that calls jsonWriteTag, change jsonWriteTag to always print the preceding comma if necessary, and print the tag only if non-NULL. diff --git src/lib/jsonWrite.c src/lib/jsonWrite.c index 9669899..ce342fc 100644 --- src/lib/jsonWrite.c +++ src/lib/jsonWrite.c @@ -53,46 +53,41 @@ if (stackIx < 0) errAbort("Stack underflow in jsonWritePopObjStack"); jw->stackIx = stackIx; } INLINE void jsonWriteMaybeComma(struct jsonWrite *jw) /* If this is not the first item added to an object or list, write a comma. */ { if (jw->objStack[jw->stackIx] != 0) dyStringAppend(jw->dy, ","JW_SEP); else jw->objStack[jw->stackIx] = 1; } void jsonWriteTag(struct jsonWrite *jw, char *var) -/* Print out quoted tag followed by colon. Print out preceding comma if need be. */ -{ -if (var != NULL) +/* Print out preceding comma if necessary, and if var is non-NULL, quoted tag followed by colon. */ { jsonWriteMaybeComma(jw); +if (var != NULL) dyStringPrintf(jw->dy, "\"%s\": ", var); } -} void jsonWriteString(struct jsonWrite *jw, char *var, char *string) /* Print out "var": "val". If var is NULL, print val only. If string is NULL, "var": null . */ { -if (var) jsonWriteTag(jw, var); -else - jsonWriteMaybeComma(jw); if (string) dyStringPrintf(jw->dy, "\"%s\"", string); else dyStringAppend(jw->dy, "null"); } void jsonWriteDateFromUnix(struct jsonWrite *jw, char *var, long long unixTimeVal) /* Add "var": YYYY-MM-DDT-HH:MM:SSZ given a Unix time stamp. Var may be NULL. */ { struct dyString *dy = jw->dy; time_t timeStamp = unixTimeVal; struct tm tm; gmtime_r(&timeStamp, &tm); jsonWriteTag(jw, var); dyStringPrintf(dy, "\"%d:%02d:%02dT%02d:%02d:%02dZ\"", @@ -141,34 +136,31 @@ dyStringAppend(dy, "["JW_SEP); jsonWritePushObjStack(jw, FALSE); } void jsonWriteListEnd(struct jsonWrite *jw) /* End an array in JSON */ { struct dyString *dy = jw->dy; dyStringAppend(dy, "]"JW_SEP); jsonWritePopObjStack(jw); } void jsonWriteObjectStart(struct jsonWrite *jw, char *var) /* Print start of object, preceded by tag if var is non-NULL. */ { -if (var) jsonWriteTag(jw, var); -else - jsonWriteMaybeComma(jw); struct dyString *dy = jw->dy; dyStringAppend(dy, "{"JW_SEP); jsonWritePushObjStack(jw, FALSE); } void jsonWriteObjectEnd(struct jsonWrite *jw) /* End object in JSON */ { struct dyString *dy = jw->dy; dyStringAppend(dy, "}"JW_SEP); jsonWritePopObjStack(jw); } void jsonWriteStringf(struct jsonWrite *jw, char *var, char *format, ...) /* Write "var": "val" where val is jsonStringEscape'd formatted string. */ @@ -214,24 +206,21 @@ jsonWriteListStart(jw, var); struct slName *sln; for (sln = slnList; sln != NULL; sln = sln->next) jsonWriteString(jw, NULL, sln->name); jsonWriteListEnd(jw); } void jsonWriteAppend(struct jsonWrite *jwA, char *var, struct jsonWrite *jwB) /* Append jwB's contents to jwA's. If jwB is non-NULL, it must be fully closed (no unclosed * list or object). If var is non-NULL, write it out as a tag before appending. * If both var and jwB are NULL, leave jwA unchanged. */ { if (jwB && jwB->stackIx) errAbort("jsonWriteAppend: second argument must be fully closed but its stackIx is %d not 0", jwB->stackIx); -if (var) jsonWriteTag(jwA, var); -else - jsonWriteMaybeComma(jwA); if (jwB) dyStringAppendN(jwA->dy, jwB->dy->string, jwB->dy->stringSize); else if (var) dyStringAppend(jwA->dy, "null"); }