Skip to content

Commit

Permalink
json: add API
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Schmidbauer committed Jan 26, 2018
1 parent f3064c6 commit a9b8682
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
57 changes: 57 additions & 0 deletions 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
31 changes: 26 additions & 5 deletions src/modules/json/json_mod.c
Expand Up @@ -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"

Expand All @@ -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}};
Expand All @@ -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) {
Expand Down

0 comments on commit a9b8682

Please sign in to comment.