Browse code

json: pass str pointer to API

Emmanuel Schmidbauer authored on 29/01/2018 19:42:29
Showing 3 changed files
... ...
@@ -26,7 +26,7 @@
26 26
 
27 27
 typedef struct json_object *(*json_parse_f) (const char *str);
28 28
 typedef struct json_object *(*json_get_object_f) (struct json_object *json_obj, const char *str);
29
-typedef str (*json_extract_field_f) (struct json_object *json_obj, char *json_name);
29
+typedef int (*json_extract_field_f) (struct json_object *json_obj, char *json_name, str *var);
30 30
 
31 31
 typedef struct json_api {
32 32
 	json_parse_f json_parse;
... ...
@@ -29,18 +29,18 @@
29 29
 
30 30
 int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst);
31 31
 
32
-#define json_extract_field(json_name, field)                                \
33
-	do {                                                                    \
32
+#define json_extract_field(json_name, field)                            \
33
+	do {                                                                \
34 34
 		struct json_object *obj = json_get_object(json_obj, json_name); \
35
-		field.s = (char *)json_object_get_string(obj);                      \
36
-		if(field.s == NULL) {                                               \
37
-			LM_DBG("Json-c error - failed to extract field [%s]\n",         \
38
-					json_name);                                             \
39
-			field.s = "";                                                   \
40
-		} else {                                                            \
41
-			field.len = strlen(field.s);                                    \
42
-		}                                                                   \
43
-		LM_DBG("%s: [%s]\n", json_name, field.s ? field.s : "Empty");       \
35
+		field->s = (char *)json_object_get_string(obj);                 \
36
+		if(field->s == NULL) {                                          \
37
+			LM_DBG("Json-c error - failed to extract field [%s]\n",     \
38
+					json_name);                                         \
39
+			field->s = "";                                              \
40
+		} else {                                                        \
41
+			field->len = strlen(field->s);                              \
42
+		}                                                               \
43
+		LM_DBG("%s: [%s]\n", json_name, field->s ? field->s : "Empty"); \
44 44
 	} while(0);
45 45
 
46 46
 
... ...
@@ -65,10 +65,9 @@ struct module_exports exports = {
65 65
 		0						 /* per-child init function */
66 66
 };
67 67
 
68
-str _json_extract_field(struct json_object *json_obj, char *json_name) {
69
-	str val = {0, 0};
68
+int _json_extract_field(struct json_object *json_obj, char *json_name, str *val) {
70 69
 	json_extract_field(json_name, val);
71
-	return val;
70
+	return 0;
72 71
 }
73 72
 
74 73
 /**