cc9a6a6dcdca9811dfeb5ac6d9f588e836407f01 larrym Tue Apr 17 23:16:38 2012 -0700 add jsonParse; refactor some existing code to accomodate parsed data diff --git src/hg/inc/jsHelper.h src/hg/inc/jsHelper.h index 2ff6ad7..1415736 100644 --- src/hg/inc/jsHelper.h +++ src/hg/inc/jsHelper.h @@ -148,55 +148,78 @@ html for communication with client side code. */ // supported types typedef enum _jsonElementType { jsonList = 0, jsonHash = 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 { jsonElementType type; struct hash *hash; }; struct jsonStringElement { jsonElementType type; char *str; }; +struct jsonBooleanElement +{ + jsonElementType type; + boolean val; +}; + +struct jsonNumberElement +{ + jsonElementType type; + long val; +}; + +struct jsonDoubleElement +{ + jsonElementType type; + double 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. void jsonHashAddDouble(struct jsonHashElement *h, char *name, double val); @@ -212,16 +235,19 @@ void jsonListAddBoolean(struct slRef **list, boolean val); void jsonPrint(struct jsonElement *json, char *name, int indentLevel); // print out a jsonElement extern struct jsonHashElement *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 */