Browse code

Merge pull request #1416 from kamailio/json-api

json: add API for json_parse and json_extract_field

Daniel-Constantin Mierla authored on 27/01/2018 15:22:19 • GitHub committed on 27/01/2018 15:22:19
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,57 @@
1
+/**
2
+ *
3
+ * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
4
+ *
5
+ * This file is part of kamailio, a free SIP server.
6
+ *
7
+ * kamailio is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * kamailio is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
+ */
21
+		       
22
+#ifndef _JSON_API_H_
23
+#define _JSON_API_H_
24
+
25
+#include "../../core/sr_module.h"
26
+
27
+typedef struct json_object *(*json_parse_f) (const char *str);
28
+typedef str (*json_extract_field_f)(
29
+		struct json_object *json_obj, char *json_name);
30
+
31
+typedef struct json_api {
32
+	json_parse_f json_parse;
33
+	json_extract_field_f extract_field;
34
+} json_api_t;
35
+
36
+typedef int (*bind_json_f) (json_api_t *api);
37
+int bind_json(json_api_t *api);
38
+
39
+/**
40
+ * @brief Load the JSON API
41
+ */
42
+static inline int json_load_api(json_api_t *api) {
43
+	bind_json_f bindjson;
44
+
45
+	bindjson = (bind_json_f)find_export("bind_json", 0, 0);
46
+	if (bindjson == 0) {
47
+		LM_ERR("cannot find bind_json\n");
48
+		return -1;
49
+	}
50
+	if (bindjson(api) < 0) {
51
+		LM_ERR("cannot bind json api\n");
52
+		return -1;
53
+	}
54
+	return 0;
55
+}
56
+
57
+#endif
... ...
@@ -27,6 +27,7 @@
27 27
 #include "../../core/mod_fix.h"
28 28
 #include "../../core/sr_module.h"
29 29
 
30
+#include "api.h"
30 31
 #include "json_funcs.h"
31 32
 #include "json_trans.h"
32 33
 
... ...
@@ -41,11 +42,11 @@ char tr_json_escape_char = '%';
41 42
 static tr_export_t mod_trans[] = {
42 43
 		{{"json", sizeof("json") - 1}, json_tr_parse}, {{0, 0}, 0}};
43 44
 
44
-static cmd_export_t cmds[]={
45
-	{"json_get_field", (cmd_function)json_get_field, 3,
46
-		fixup_get_field, fixup_get_field_free, ANY_ROUTE},
47
-	{0, 0, 0, 0, 0, 0}
48
-};
45
+static cmd_export_t cmds[] = {
46
+		{"json_get_field", (cmd_function)json_get_field, 3, fixup_get_field,
47
+				fixup_get_field_free, ANY_ROUTE},
48
+		{"bind_json", (cmd_function)bind_json, 0, 0, 0, ANY_ROUTE},
49
+		{0, 0, 0, 0, 0, 0}};
49 50
 
50 51
 static param_export_t params[] = {
51 52
 		{"json_escape_char", PARAM_STR, &tr_json_escape_str}, {0, 0, 0}};
... ...
@@ -64,6 +65,26 @@ struct module_exports exports = {
64 65
 		0						 /* per-child init function */
65 66
 };
66 67
 
68
+str _json_extract_field(struct json_object *json_obj, char *json_name)
69
+{
70
+	str val;
71
+	json_extract_field(json_name, val);
72
+	return val;
73
+}
74
+
75
+/**
76
+ *
77
+ */
78
+int bind_json(json_api_t *api) {
79
+	if (!api) {
80
+		ERR("Invalid parameter value\n");
81
+		return -1;
82
+	}
83
+	api->json_parse = json_parse;
84
+	api->extract_field = _json_extract_field;
85
+	return 0;
86
+}
87
+
67 88
 int mod_register(char *path, int *dlflags, void *p1, void *p2)
68 89
 {
69 90
 	if(json_tr_init_buffers() < 0) {