ffc338d50d807987ff5a22ccfdd8a59f660e70d2 kent Sun Jul 2 08:51:10 2017 -0700 Making JW_SEP not a define any more but a field of he jsonWrite structure. diff --git src/lib/jsonWrite.c src/lib/jsonWrite.c index 5be896f..9ee7bf7 100644 --- src/lib/jsonWrite.c +++ src/lib/jsonWrite.c @@ -2,38 +2,38 @@ /* Copyright (C) 2014 The Regents of the University of California * See README in this or parent directory for licensing information. */ #include "common.h" #include "hash.h" #include "dystring.h" #include "sqlNum.h" #include "jsonParse.h" #include "jsonWrite.h" // Separator between elements; set this to "\n" to see elements on separate lines. // Newlines are fine in Javascript, e.g. in an embedded <script>. // However, unescaped \n is illegal in JSON and web browsers may reject it. // Web browser plugins can pretty-print JSON nicely. -#define JW_SEP " " struct jsonWrite *jsonWriteNew() /* Return new empty jsonWrite struct. */ { struct jsonWrite *jw; AllocVar(jw); jw->dy = dyStringNew(0); +jw->sep = ' '; return jw; } void jsonWriteFree(struct jsonWrite **pJw) /* Free up a jsonWrite object. */ { struct jsonWrite *jw = *pJw; if (jw != NULL) { dyStringFree(&jw->dy); freez(pJw); } } static void jsonWritePushObjStack(struct jsonWrite *jw, bool isNotEmpty, bool isObject) @@ -53,31 +53,34 @@ boolean topIsObject = jw->objStack[jw->stackIx].isObject; if (topIsObject != isObject) errAbort("jsonWrite: expected to close %s but was told to close %s", topIsObject ? "object" : "list", isObject ? "object" : "list"); int stackIx = jw->stackIx - 1; 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].isNotEmpty) - dyStringAppend(jw->dy, ","JW_SEP); + { + dyStringAppendC(jw->dy, ','); + dyStringAppendC(jw->dy, jw->sep); + } else jw->objStack[jw->stackIx].isNotEmpty = TRUE; } void jsonWriteTag(struct jsonWrite *jw, char *var) /* 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 . */ { @@ -127,56 +130,60 @@ void jsonWriteLinkNum(struct jsonWrite *jw, char *var, char *objRoot, long long id) /* Print out the jsony type link to another object with a numerical id. objRoot will start * and end with a '/' and may have additional slashes in this usage. Var may be NULL */ { struct dyString *dy = jw->dy; jsonWriteTag(jw, var); dyStringPrintf(dy, "\"%s%lld\"", objRoot, id); } void jsonWriteListStart(struct jsonWrite *jw, char *var) /* Start an array in JSON. Var may be NULL */ { struct dyString *dy = jw->dy; jsonWriteTag(jw, var); -dyStringAppend(dy, "["JW_SEP); +dyStringAppendC(dy, '['); +dyStringAppendC(dy, jw->sep); jsonWritePushObjStack(jw, FALSE, FALSE); } void jsonWriteListEnd(struct jsonWrite *jw) /* End an array in JSON */ { struct dyString *dy = jw->dy; -dyStringAppend(dy, "]"JW_SEP); +dyStringAppendC(dy, ']'); +dyStringAppendC(dy, jw->sep); jsonWritePopObjStack(jw, FALSE); } void jsonWriteObjectStart(struct jsonWrite *jw, char *var) /* Print start of object, preceded by tag if var is non-NULL. */ { jsonWriteTag(jw, var); struct dyString *dy = jw->dy; -dyStringAppend(dy, "{"JW_SEP); +dyStringAppendC(dy, '{'); +dyStringAppendC(dy, jw->sep); jsonWritePushObjStack(jw, FALSE, TRUE); } void jsonWriteObjectEnd(struct jsonWrite *jw) /* End object in JSON */ { struct dyString *dy = jw->dy; -dyStringAppend(dy, "}"JW_SEP); +dyStringAppendC(dy, '}'); +dyStringAppendC(dy, jw->sep); jsonWritePopObjStack(jw, TRUE); } void jsonWriteStringf(struct jsonWrite *jw, char *var, char *format, ...) /* Write "var": "val" where val is jsonStringEscape'd formatted string. */ { // Since we're using jsonStringEscape(), we need to use a temporary dyString // instead of jw->dy. struct dyString *tmpDy = dyStringNew(0); va_list args; va_start(args, format); dyStringVaPrintf(tmpDy, format, args); va_end(args); char *escaped = jsonStringEscape(tmpDy->string); jsonWriteString(jw, var, escaped);