Module: kamailio
Branch: master
Commit: bff50c44e32c48b5449e405f67ca02f7d8055ba0
URL:
https://github.com/kamailio/kamailio/commit/bff50c44e32c48b5449e405f67ca02f…
Author: Emmanuel Schmidbauer <eschmidbauer(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2018-03-02T15:17:03+01:00
json: add new function json_get_string() to return string value without quotes
---
Modified: src/modules/json/doc/json_admin.xml
Modified: src/modules/json/json_funcs.c
Modified: src/modules/json/json_funcs.h
Modified: src/modules/json/json_mod.c
---
Diff:
https://github.com/kamailio/kamailio/commit/bff50c44e32c48b5449e405f67ca02f…
Patch:
https://github.com/kamailio/kamailio/commit/bff50c44e32c48b5449e405f67ca02f…
---
diff --git a/src/modules/json/doc/json_admin.xml b/src/modules/json/doc/json_admin.xml
index d510090285..72c7cb1344 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)");
+...
+ </programlisting>
+ </example>
+ </section>
+ <section id="json.f.json_get_string">
+ <title>
+ <function moreinfo="none">json_get_string(json_string, field_name,
destination)</function>
+ </title>
+ <para>
+ Copy field 'field_name' from json object 'json_string' and store it in
pvar 'destination'.
+ </para>
+ <para>
+ </para>
+ <example>
+ <title><function>json_get_string</function> usage</title>
+ <programlisting format="linespecific">
+...
+json_get_string("{'foo':'bar'}", "foo",
"$var(foo)");
+xlog("foo is $var(foo)");
...
</programlisting>
</example>
diff --git a/src/modules/json/json_funcs.c b/src/modules/json/json_funcs.c
index ae036ac1f1..360bfc172c 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 747232efac..60b67e037b 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 <json.h>
+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 f5de53d692..91452b3a55 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}};