From bc1e60b2510173521d43ec623046a94f16e363da Mon Sep 17 00:00:00 2001 From: Emmanuel Schmidbauer Date: Thu, 1 Mar 2018 15:22:06 -0500 Subject: [PATCH] json: add new function json_get_string() to return string value without quotes --- src/modules/json/doc/json_admin.xml | 19 +++++++++++++++++++ src/modules/json/json_funcs.c | 20 +++++++++++++++++--- src/modules/json/json_funcs.h | 7 +++++++ src/modules/json/json_mod.c | 2 ++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/modules/json/doc/json_admin.xml b/src/modules/json/doc/json_admin.xml index d510090285f..72c7cb1344f 100644 --- a/src/modules/json/doc/json_admin.xml +++ b/src/modules/json/doc/json_admin.xml @@ -75,6 +75,25 @@ ... json_get_field("{'foo':'bar'}", "foo", "$var(foo)"); xlog("foo is $var(foo)"); +... + + + +
+ + <function moreinfo="none">json_get_string(json_string, field_name, destination)</function> + + + Copy field 'field_name' from json object 'json_string' and store it in pvar 'destination'. + + + + + <function>json_get_string</function> usage + +... +json_get_string("{'foo':'bar'}", "foo", "$var(foo)"); +xlog("foo is $var(foo)"); ... diff --git a/src/modules/json/json_funcs.c b/src/modules/json/json_funcs.c index ae036ac1f1d..360bfc172c4 100644 --- a/src/modules/json/json_funcs.c +++ b/src/modules/json/json_funcs.c @@ -30,7 +30,7 @@ #include "json_funcs.h" -int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst) +int _json_get_field(struct sip_msg *msg, char *json, char *field, char *dst, int field_type) { str json_s; str field_s; @@ -53,7 +53,6 @@ int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst) dst_pv = (pv_spec_t *)dst; - j = json_tokener_parse(json_s.s); if (j==NULL) { @@ -63,7 +62,11 @@ int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst) json_object_object_get_ex(j, field_s.s, &oj); if(oj!=NULL) { - value = (char*)json_object_to_json_string(oj); + if (field_type == JSON_FIELD_STRING) { + value = (char*)json_object_get_string(oj); + } else { + value = (char*)json_object_to_json_string(oj); + } dst_val.rs.s = value; dst_val.rs.len = strlen(value); dst_val.flags = PV_VAL_STR; @@ -77,6 +80,17 @@ int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst) return ret; } +int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst) +{ + return _json_get_field(msg, json, field, dst, JSON_FIELD_DEFAULT); +} + + +int json_get_string(struct sip_msg* msg, char* json, char* field, char* dst) +{ + return _json_get_field(msg, json, field, dst, JSON_FIELD_STRING); +} + #define json_foreach_key(obj, key) \ char *key; \ struct lh_entry *entry##key; \ diff --git a/src/modules/json/json_funcs.h b/src/modules/json/json_funcs.h index 747232efac9..60b67e037b2 100644 --- a/src/modules/json/json_funcs.h +++ b/src/modules/json/json_funcs.h @@ -27,7 +27,14 @@ #include "../../core/parser/msg_parser.h" #include +enum _json_get_field_type +{ + JSON_FIELD_DEFAULT = 0, + JSON_FIELD_STRING +}; + int json_get_field(struct sip_msg* msg, char* json, char* field, char* dst); +int json_get_string(struct sip_msg* msg, char* json, char* field, char* dst); #define json_extract_field(json_name, field) \ do { \ diff --git a/src/modules/json/json_mod.c b/src/modules/json/json_mod.c index f5de53d6920..91452b3a554 100644 --- a/src/modules/json/json_mod.c +++ b/src/modules/json/json_mod.c @@ -45,6 +45,8 @@ static tr_export_t mod_trans[] = { static cmd_export_t cmds[] = { {"json_get_field", (cmd_function)json_get_field, 3, fixup_get_field, fixup_get_field_free, ANY_ROUTE}, + {"json_get_string", (cmd_function)json_get_string, 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}};