16fdc50e4e49b031f566911bf0371d4942282d75 larrym Fri Apr 27 16:22:37 2012 -0700 use a union in jsonElement to simplify code and eliminate typecasts (per suggestion of angie and tim); rename jsonHash to jsonObject; fix some obsolete comments diff --git src/hg/inc/jsHelper.h src/hg/inc/jsHelper.h index 1415736..1bf233b 100644 --- src/hg/inc/jsHelper.h +++ src/hg/inc/jsHelper.h @@ -141,113 +141,71 @@ /* Make the hidden input, collapse/expand button and <TR id=...> needed for utils.js's * setTableRowVisibility(). Caller needs to have already created a <TABLE> and <FORM>. */ void jsEndCollapsibleSection(); /* End the collapsible <TR id=...>. */ /* JSON Element code let's you build up a DOM like data structure in memory and then serialize it into html for communication with client side code. */ // supported types typedef enum _jsonElementType { jsonList = 0, - jsonHash = 1, + jsonObject = 1, jsonNumber = 2, jsonDouble = 3, jsonBoolean = 4, jsonString = 5 } jsonElementType; -// May turn the following into a union to avoid casts (still debating that with angie). - -struct jsonElement -{ - jsonElementType type; - // rest of data is here. -}; - -struct jsonListElement -{ - jsonElementType type; - struct slRef *list; -}; - -struct jsonHashElement +union jsonElementVal { - jsonElementType type; - struct hash *hash; + struct slRef *jeList; + struct hash *jeHash; + long jeNumber; + double jeDouble; + boolean jeBoolean; + char *jeString; }; -struct jsonStringElement -{ - jsonElementType type; - char *str; -}; - -struct jsonBooleanElement -{ - jsonElementType type; - boolean val; -}; - -struct jsonNumberElement -{ - jsonElementType type; - long val; -}; - -struct jsonDoubleElement +struct jsonElement { jsonElementType type; - double val; + union jsonElementVal val; }; -struct jsonStringElement *newJsonString(char *str); -struct jsonBooleanElement *newJsonBoolean(boolean val); -struct jsonNumberElement *newJsonNumber(long val); -struct jsonDoubleElement *newJsonDouble(double val); -struct jsonHashElement *newJsonHash(struct hash *h); -struct jsonListElement *newJsonList(struct slRef *list); - -// NOTE: Adding to a NULL hash will add to the global "common" hash printed with jsonPrintGlobals() -void jsonHashAdd(struct jsonHashElement *h, char *name, struct jsonElement *ele); - -void jsonHashAddString(struct jsonHashElement *h, char *name, char *val); -// Add a string to a hash which will be used to print a javascript object; -// existing values are replaced. - -void jsonHashAddNumber(struct jsonHashElement *h, char *name, long val); -// Add a number to a hash which will be used to print a javascript object; -// existing values are replaced. +// constructors for each jsonElementType -void jsonHashAddDouble(struct jsonHashElement *h, char *name, double val); +struct jsonElement *newJsonString(char *str); +struct jsonElement *newJsonBoolean(boolean val); +struct jsonElement *newJsonNumber(long val); +struct jsonElement *newJsonDouble(double val); +struct jsonElement *newJsonObject(struct hash *h); +struct jsonElement *newJsonList(struct slRef *list); -void jsonHashAddBoolean(struct jsonHashElement *h, char *name, boolean val); -// Add a boolean to a hash which will be used to print a javascript object; -// existing values are replaced. +void jsonObjectAdd(struct jsonElement *h, char *name, struct jsonElement *ele); +// Add a new element to a jsonObject; existing values are replaced. +// NOTE: Adding to a NULL hash will add to the global "common" hash printed with jsonPrintGlobals(); -void jsonListAdd(struct slRef **list, struct jsonElement *ele); -void jsonListAddString(struct slRef **list, char *val); -void jsonListAddNumber(struct slRef **list, long val); -void jsonListAddDouble(struct slRef **list, double val); -void jsonListAddBoolean(struct slRef **list, boolean val); +void jsonListAdd(struct jsonElement *list, struct jsonElement *ele); +// Add a new element to a jsonList void jsonPrint(struct jsonElement *json, char *name, int indentLevel); // print out a jsonElement -extern struct jsonHashElement *jsonGlobalsHash; // The "all" globals json hash +extern struct jsonElement *jsonGlobalsHash; // The "all" globals json hash void jsonPrintGlobals(boolean wrapWithScriptTags); // prints out the "common" globals json hash // This hash is the one utils.js and therefore all CGIs know about 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} struct jsonElement *jsonParse(char *str); // parse string into an in-memory json representation #endif /* JSHELPER_H */