Browse Source

Helper functions bfg_json_obj_string and share __json_array_string

Luke Dashjr 12 years ago
parent
commit
71ede4ca03
2 changed files with 13 additions and 1 deletions
  1. 1 1
      util.c
  2. 12 0
      util.h

+ 1 - 1
util.c

@@ -1594,7 +1594,7 @@ char *json_dumps_ANY(json_t *json, size_t flags)
 /* Extracts a string value from a json array with error checking. To be used
 /* Extracts a string value from a json array with error checking. To be used
  * when the value of the string returned is only examined and not to be stored.
  * when the value of the string returned is only examined and not to be stored.
  * See json_array_string below */
  * See json_array_string below */
-static char *__json_array_string(json_t *val, unsigned int entry)
+char *__json_array_string(json_t *val, unsigned int entry)
 {
 {
 	json_t *arr_entry;
 	json_t *arr_entry;
 
 

+ 12 - 0
util.h

@@ -14,6 +14,7 @@
 #define __UTIL_H__
 #define __UTIL_H__
 
 
 #include <stdbool.h>
 #include <stdbool.h>
+#include <string.h>
 #include <sys/time.h>
 #include <sys/time.h>
 
 
 #include <curl/curl.h>
 #include <curl/curl.h>
@@ -74,6 +75,17 @@
 #endif
 #endif
 extern char *json_dumps_ANY(json_t *, size_t flags);
 extern char *json_dumps_ANY(json_t *, size_t flags);
 
 
+static inline
+const char *bfg_json_obj_string(json_t *json, const char *key, const char *fail)
+{
+	json = json_object_get(json, key);
+	if (!json)
+		return fail;
+	return json_string_value(json) ?: fail;
+}
+
+extern char *__json_array_string(json_t *, unsigned int entry);
+
 static inline
 static inline
 bool isCspace(int c)
 bool isCspace(int c)
 {
 {