Module: kamailio Branch: master Commit: a9b8682f522f46c56144e61ff029cb1ab0413274 URL: https://github.com/kamailio/kamailio/commit/a9b8682f522f46c56144e61ff029cb1a...
Author: Emmanuel Schmidbauer emmanuel@getweave.com Committer: Emmanuel Schmidbauer emmanuel@getweave.com Date: 2018-01-26T09:20:40-07:00
json: add API
---
Added: src/modules/json/api.h Modified: src/modules/json/json_mod.c
---
Diff: https://github.com/kamailio/kamailio/commit/a9b8682f522f46c56144e61ff029cb1a... Patch: https://github.com/kamailio/kamailio/commit/a9b8682f522f46c56144e61ff029cb1a...
---
diff --git a/src/modules/json/api.h b/src/modules/json/api.h new file mode 100644 index 0000000000..80d18c2c25 --- /dev/null +++ b/src/modules/json/api.h @@ -0,0 +1,57 @@ +/** + * + * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com) + * + * This file is part of kamailio, a free SIP server. + * + * kamailio is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version + * + * kamailio is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _JSON_API_H_ +#define _JSON_API_H_ + +#include "../../core/sr_module.h" + +typedef struct json_object *(*json_parse_f) (const char *str); +typedef str (*json_extract_field_f)( + struct json_object *json_obj, char *json_name); + +typedef struct json_api { + json_parse_f json_parse; + json_extract_field_f extract_field; +} json_api_t; + +typedef int (*bind_json_f) (json_api_t *api); +int bind_json(json_api_t *api); + +/** + * @brief Load the JSON API + */ +static inline int json_load_api(json_api_t *api) { + bind_json_f bindjson; + + bindjson = (bind_json_f)find_export("bind_json", 0, 0); + if (bindjson == 0) { + LM_ERR("cannot find bind_json\n"); + return -1; + } + if (bindjson(api) < 0) { + LM_ERR("cannot bind json api\n"); + return -1; + } + return 0; +} + +#endif diff --git a/src/modules/json/json_mod.c b/src/modules/json/json_mod.c index 4ab9ab1829..a008c13085 100644 --- a/src/modules/json/json_mod.c +++ b/src/modules/json/json_mod.c @@ -27,6 +27,7 @@ #include "../../core/mod_fix.h" #include "../../core/sr_module.h"
+#include "api.h" #include "json_funcs.h" #include "json_trans.h"
@@ -41,11 +42,11 @@ char tr_json_escape_char = '%'; static tr_export_t mod_trans[] = { {{"json", sizeof("json") - 1}, json_tr_parse}, {{0, 0}, 0}};
-static cmd_export_t cmds[]={ - {"json_get_field", (cmd_function)json_get_field, 3, - fixup_get_field, fixup_get_field_free, ANY_ROUTE}, - {0, 0, 0, 0, 0, 0} -}; +static cmd_export_t cmds[] = { + {"json_get_field", (cmd_function)json_get_field, 3, fixup_get_field, + fixup_get_field_free, ANY_ROUTE}, + {"bind_json", (cmd_function)bind_json, 0, 0, 0, ANY_ROUTE}, + {0, 0, 0, 0, 0, 0}};
static param_export_t params[] = { {"json_escape_char", PARAM_STR, &tr_json_escape_str}, {0, 0, 0}}; @@ -64,6 +65,26 @@ struct module_exports exports = { 0 /* per-child init function */ };
+str _json_extract_field(struct json_object *json_obj, char *json_name) +{ + str val; + json_extract_field(json_name, val); + return val; +} + +/** + * + */ +int bind_json(json_api_t *api) { + if (!api) { + ERR("Invalid parameter value\n"); + return -1; + } + api->json_parse = json_parse; + api->extract_field = _json_extract_field; + return 0; +} + int mod_register(char *path, int *dlflags, void *p1, void *p2) { if(json_tr_init_buffers() < 0) {