Module: sip-router
Branch: master
Commit: fcb2eef025b7272d1c5e2ff14e880d169a6bf528
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=fcb2eef…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: Fri Aug 15 21:52:52 2014 +0200
jsonrpc-s: handle struct_add() to an array node
- wraps each member in an object and adds to array (similar to xmlrpc
behaviour)
---
modules/jsonrpc-s/jsonrpc-s_mod.c | 23 +++++++++++++++++++----
1 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/modules/jsonrpc-s/jsonrpc-s_mod.c b/modules/jsonrpc-s/jsonrpc-s_mod.c
index cdb54ea..1fb2dd9 100644
--- a/modules/jsonrpc-s/jsonrpc-s_mod.c
+++ b/modules/jsonrpc-s/jsonrpc-s_mod.c
@@ -530,19 +530,23 @@ static int jsonrpc_printf(jsonrpc_ctx_t* ctx, char* fmt, ...)
static int jsonrpc_struct_add(srjson_t *jnode, char* fmt, ...)
{
srjson_t *nj = NULL;
+ srjson_t *wj = NULL;
jsonrpc_ctx_t* ctx;
va_list ap;
void **void_ptr;
str mname;
+ int isobject;
if(jnode==NULL) {
LM_ERR("invalid json node parameter\n");
return -1;
}
- if(jnode->type!=srjson_Object) {
- LM_ERR("json node parameter is not object (%d)\n", jnode->type);
+ if(jnode->type!=srjson_Object && jnode->type!=srjson_Array) {
+ LM_ERR("json node parameter is not object or array (%d)\n",
+ jnode->type);
return -1;
}
+ isobject = (jnode->type==srjson_Object);
ctx = &_jsonrpc_ctx;
if(ctx->jrpl==NULL) {
@@ -568,8 +572,19 @@ static int jsonrpc_struct_add(srjson_t *jnode, char* fmt, ...)
}
if(nj==NULL) goto err;
- srjson_AddItemToObject(ctx->jrpl, jnode,
- mname.s, nj);
+ if(isobject) {
+ /* add as member to object */
+ srjson_AddItemToObject(ctx->jrpl, jnode, mname.s, nj);
+ } else {
+ /* wrap member in a new object and add to array */
+ wj = srjson_CreateObject(ctx->jrpl);
+ if(wj==NULL) {
+ srjson_Delete(ctx->jrpl, nj);
+ goto err;
+ }
+ srjson_AddItemToObject(ctx->jrpl, wj, mname.s, nj);
+ srjson_AddItemToArray(ctx->jrpl, jnode, wj);
+ }
fmt++;
}
va_end(ap);