e53ecf333db04604a76787d45552f4c392585859
chinhli
  Wed Oct 2 12:14:17 2013 -0700
Added jsonFindNameRecurse() and jsonFindName() to extract the email address from JSON file.
diff --git src/hg/lib/jsHelper.c src/hg/lib/jsHelper.c
index 9b37c25..0ac6f91 100644
--- src/hg/lib/jsHelper.c
+++ src/hg/lib/jsHelper.c
@@ -983,15 +983,77 @@
         case '\\':
         case '/':
         case '\b':
         case '\f':
         case '\n':
         case '\r':
         case '\t':
             *out++ = '\\';
             break;
         }
     *out++ = c;
     }
 *out++ = 0;
 return outString;
 }
+
+void jsonFindNameRecurse(struct jsonElement *ele, char *jName, struct slName *sn)
+// Search the JSON tree recursively to find all the values associated to
+// the name. Value found are added to the tail of the  slName list 
+{
+switch (ele->type)
+    {
+    case jsonObject:
+        {
+        if(hashNumEntries(ele->val.jeHash))
+            {
+            struct hashEl *el, *list = hashElListHash(ele->val.jeHash);
+            slSort(&list, hashElCmp);
+            for (el = list; el != NULL; el = el->next)
+                {
+                struct jsonElement *val = el->val;
+                if sameString(el->name, jName)
+                    slNameAddTail(&sn, jsonStringEscape(val->val.jeString));
+                jsonFindNameRecurse(val, jName, sn);
+                }
+            hashElFreeList(&list);
+            }
+        break;
+        }
+    case jsonList:
+        {
+        struct slRef *el;
+        if(ele->val.jeList)
+            {
+            for (el = ele->val.jeList; el != NULL; el = el->next)
+                {
+                struct jsonElement *val = el->val;
+                jsonFindNameRecurse(val, jName, sn);
+                }
+            }
+        break;
+        }
+    case jsonString:
+    case jsonBoolean:
+    case jsonNumber:
+    case jsonDouble:
+        {
+        break;
+        }
+    default:
+        {
+        errAbort("jsonFindNameRecurse; invalid type: %d", ele->type);
+        break;
+        }
+    }
+}
+
+struct slName *jsonFindName(struct jsonElement *json, char *jName)
+// Search the JSON tree to find all the values associated to the name
+// and  put them into a slName list. The first element of the list is the
+// name itself and values are added to the tail of the list.  
+{
+struct slName *sn = slNameNew(jName);
+jsonFindNameRecurse(json, jName, sn);
+return sn;
+}
+