Module: sip-router
Branch: mgw/json
Commit: b633090d7eb5c2a8c42ffb8224520bc296409713
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=b633090…
Author: Matthew Williams <mgwilliams(a)gmail.com>
Committer: Matthew Williams <mgwilliams(a)gmail.com>
Date: Mon Jan 28 13:18:55 2013 -0800
json: Support arrays as top level json objects.
---
modules/json/README | 2 +-
modules/json/doc/json_admin.xml | 2 +-
modules/json/json_funcs.c | 5 ++-
modules/json/json_path.c | 5 +++
modules/json/test/json-test.cfg | 61 ++++++++++++++++++++++++++++++--------
5 files changed, 59 insertions(+), 16 deletions(-)
diff --git a/modules/json/README b/modules/json/README
index 30cabb5..5c64e6b 100644
--- a/modules/json/README
+++ b/modules/json/README
@@ -97,7 +97,7 @@ Chapter 1. Admin Guide
in pvar 'destination'.
The path string supports dot delimited notation (e.g. foo.bar.baz),
- array notation (e.g. list[0]), or a combination of the two (e.g.
+ array notation (e.g. [0]), or a combination of the two (e.g.
foo.bar[0][1].baz).
The function can put a string, integer, null, or new json string into
diff --git a/modules/json/doc/json_admin.xml b/modules/json/doc/json_admin.xml
index c552bb5..17ef15d 100644
--- a/modules/json/doc/json_admin.xml
+++ b/modules/json/doc/json_admin.xml
@@ -69,7 +69,7 @@
Copy the value at the path from json object 'json_string' and store
it in pvar 'destination'.
</para>
<para>
- The path string supports dot delimited notation (e.g. foo.bar.baz), array
notation (e.g. list[0]), or a combination of the two (e.g. foo.bar[0][1].baz).
+ The path string supports dot delimited notation (e.g. foo.bar.baz), array
notation (e.g. [0]), or a combination of the two (e.g. foo.bar[0][1].baz).
</para>
<para>
The function can put a string, integer, null, or new json string into
destination.
diff --git a/modules/json/json_funcs.c b/modules/json/json_funcs.c
index 23b45fd..ef1e858 100644
--- a/modules/json/json_funcs.c
+++ b/modules/json/json_funcs.c
@@ -77,7 +77,7 @@ int jsonmod_path_get(struct sip_msg* msg, char* json_in, char* path_in,
char* ds
char* freeme = NULL;
- if(json_is_object(v)) {
+ if(json_is_object(v) || json_is_array(v)) {
const char* value = json_dumps(v, JSON_COMPACT);
freeme = (char*)value;
dst_val.rs.s = (char*)value;
@@ -88,6 +88,9 @@ int jsonmod_path_get(struct sip_msg* msg, char* json_in, char* path_in,
char* ds
dst_val.rs.s = (char*)value;
dst_val.rs.len = strlen(value);
dst_val.flags = PV_VAL_STR;
+ }else if(json_is_boolean(v)) {
+ dst_val.ri = json_is_true(v) ? 0 : 1;
+ dst_val.flags |= PV_TYPE_INT|PV_VAL_INT;
}else if(json_is_integer(v)) {
int value = json_integer_value(v);
dst_val.ri = value;
diff --git a/modules/json/json_path.c b/modules/json/json_path.c
index 4dbfd1f..5d40cae 100644
--- a/modules/json/json_path.c
+++ b/modules/json/json_path.c
@@ -60,6 +60,11 @@ json_t *json_path_get(const json_t *json, const char *path)
cursor = json;
expect = path_delims;
+ if (*token == array_open) {
+ expect = array_close;
+ token++;
+ }
+
while (peek && *peek && cursor)
{
char *last_peek = peek;
diff --git a/modules/json/test/json-test.cfg b/modules/json/test/json-test.cfg
index fb815b2..4b487b9 100644
--- a/modules/json/test/json-test.cfg
+++ b/modules/json/test/json-test.cfg
@@ -47,10 +47,10 @@ event_route[htable:mod-init]
$var(badjson) = "some string thing";
$var(array_json_objs) =
"{\"list\":[{\"foo\":\"bar\"},{\"baz\":\"derp\"},{\"herp\":\"blarg\"}]}";
$var(null_json) = "{\"null\":null}";
- $var(array_json) = "{\"loopme\":[0,1,2,3,4,5]}";
- $var(empty_array) = "{\"list\":[]}";
+ $var(array_json) = "[0,1,2,3,4,5]";
+ $var(empty_array) = "[]";
- $var(failed)=0;
+ $var(failed) = 0;
# testA
json_path_get("{\"foo\":\"bar\"}","foo","$var(testA)");
@@ -130,14 +130,14 @@ event_route[htable:mod-init]
}
# testK
- json_array_size($var(array_json), "loopme", "$var(testK)");
+ json_array_size($var(array_json), "", "$var(testK)");
if($var(testK) != 6) {
xlog("L_ALERT","FAILED testK: size of loopme is
\"$var(testK)\"\n");
$var(failed) = 1;
}
# testL
- json_array_size($var(empty_array), "list", "$var(testL)");
+ json_array_size($var(empty_array), "", "$var(testL)");
if($var(testL) != 0) {
xlog("L_ALERT","FAILED testL: size is
\"$var(testL)\"\n");
$var(failed) = 1;
@@ -146,11 +146,11 @@ event_route[htable:mod-init]
# testM
# looping test
$var(count) = 0;
- json_array_size($var(array_json), "loopme", "$var(testM_size)");
+ json_array_size($var(array_json), "", "$var(testM_size)");
while($var(count) < $var(testM_size)) {
- json_path_get($var(array_json), "loopme[$var(count)]",
"$var(testM)");
+ json_path_get($var(array_json), "[$var(count)]",
"$var(testM)");
if($var(testM) != $var(count)) {
- xlog("L_ALERT","FAILED testM:
\"loopme[$var(count)]\" == \"$var(testM)\"\n");
+ xlog("L_ALERT","FAILED testM: \"[$var(count)]\" ==
$var(testM)\n");
$var(failed) = 1;
}
$var(count) = $var(count) + 1;
@@ -166,23 +166,58 @@ event_route[htable:mod-init]
# testN
$var(foo_json) = "{\"foo\":\"bar\"}";
json_path_get($var(foo_json),"","$var(testN)");
- if($var(testN)!=$var(foo_json)) {
+ if($var(testN) != $var(foo_json)) {
xlog("L_ALERT","FAILED testN: \"foo\" ==
\"$var(testN)\"\n");
$var(failed) = 1;
}
# testO
json_get_field("{\"foo\":\"bar\"}", "foo",
"$var(testO)");
- if($var(testO)!="bar") {
+ if($var(testO) != "bar") {
xlog("L_ALERT","FAILED testO: \"foo\" ==
\"$var(testO)\"\n");
$var(failed) = 1;
}
- if($var(failed) == 1) {
- xlog("L_ALERT", "JSON TESTS FAILED\n");
+ # testP
+ json_get_field("{\"false\":false}", "false",
"$var(testP)");
+ if(!$var(testP)) {
+ xlog("L_ALERT","FAILED testP: \"false\" ==
$var(testP)\n");
+ $var(failed) = 1;
+ }
+
+ # testQ
+ json_get_field("{\"true\":true}", "true",
"$var(testQ)");
+ if($var(testQ)) {
+ xlog("L_ALERT","FAILED testQ: \"true\" ==
$var(testQ)\n");
+ $var(failed) = 1;
+ }
+
+ # testR
+ # negative
+ json_get_field("{\"neg\":-100}", "neg",
"$var(testR)");
+ if($var(testR)!=-100) {
+ xlog("L_ALERT","FAILED testR: \"neg\" ==
$var(testR)\n");
+ $var(failed) = 1;
}
- if($var(failed) == 0) {
+ # testS
+ # large negative
+ json_get_field("{\"neg\":-32768}", "neg",
"$var(testS)");
+ if($var(testS)!=-32768) {
+ xlog("L_ALERT","FAILED testS: \"neg\" ==
$var(testS)\n");
+ $var(failed) = 1;
+ }
+
+ # testT
+ json_get_field($var(myjson), "inner.deep.list", "$var(testT)");
+ if($var(testT)!="[1,2,3,4]") {
+ xlog("L_ALERT","FAILED testT: \"inner.deep.list\" ==
$var(testT)\n");
+ $var(failed) = 1;
+ }
+
+ if($var(failed)) {
+ xlog("L_ALERT", "JSON TESTS FAILED\n");
+ } else {
xlog("L_ALERT", "JSON TESTS PASSED\n");
}