int _cfgt_get_obj_avp_vals(str name, sr_xavp_t *xavp, srjson_doc_t *jdoc, srjson_t **jobj) { sr_xavp_t *avp = NULL; srjson_t *jobjt = NULL; *jobj = srjson_CreateArray(jdoc); if(*jobj==NULL) { LM_ERR("cannot create json object\n"); return -1; } avp = xavp; while(avp!=NULL&&!STR_EQ(avp->name,name)) { avp = avp->next; } while(avp!=NULL) { _cfgt_get_obj_xavp_val(avp, jdoc, &jobjt); srjson_AddItemToArray(jdoc, *jobj, jobjt); jobjt = NULL; avp = xavp_get_next(avp); } return 0; }
void srjson_AddStrItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, int len, srjson_t *item) { if (!item) return; if (item->string) doc->free_fn(item->string); item->string = srjson_strndupz(doc, string, len); srjson_AddItemToArray(doc, object, item); }
int _cfgt_get_array_avp_vals(struct sip_msg *msg, pv_param_t *param, srjson_doc_t *jdoc, srjson_t **jobj, str *item_name) { struct usr_avp *avp; unsigned short name_type; int_str avp_name; int_str avp_value; struct search_state state; srjson_t *jobjt; memset(&state, 0, sizeof(struct search_state)); if(pv_get_avp_name(msg, param, &avp_name, &name_type)!=0) { LM_ERR("invalid name\n"); return -1; } *jobj = srjson_CreateArray(jdoc); if(*jobj==NULL) { LM_ERR("cannot create json object\n"); return -1; } if ((avp=search_first_avp(name_type, avp_name, &avp_value, &state))==0) { goto ok; } do { if(avp->flags & AVP_VAL_STR) { jobjt = srjson_CreateStr(jdoc, avp_value.s.s, avp_value.s.len); if(jobjt==NULL) { LM_ERR("cannot create json object\n"); return -1; } } else { jobjt = srjson_CreateNumber(jdoc, avp_value.n); if(jobjt==NULL) { LM_ERR("cannot create json object\n"); return -1; } } srjson_AddItemToArray(jdoc, *jobj, jobjt); } while ((avp=search_next_avp(&state, &avp_value))!=0); ok: item_name->s = avp_name.s.s; item_name->len = avp_name.s.len; return 0; }
int _cfgt_get_obj_xavp_vals(struct sip_msg *msg, pv_param_t *param, srjson_doc_t *jdoc, srjson_t **jobjr, str *item_name) { pv_xavp_name_t *xname = (pv_xavp_name_t*)param->pvn.u.dname; sr_xavp_t *xavp = NULL; sr_xavp_t *avp = NULL; srjson_t *jobj = NULL; srjson_t *jobjt = NULL; struct str_list *keys; struct str_list *k; *jobjr = srjson_CreateArray(jdoc); if(*jobjr==NULL) { LM_ERR("cannot create json object\n"); return -1; } item_name->s = xname->name.s; item_name->len = xname->name.len; xavp = xavp_get_by_index(&xname->name, 0, NULL); if(xavp==NULL) { return 0; /* empty */ } do { if(xavp->val.type==SR_XTYPE_XAVP) { avp = xavp->val.v.xavp; jobj = srjson_CreateObject(jdoc); if(jobj==NULL) { LM_ERR("cannot create json object\n"); return -1; } keys = xavp_get_list_key_names(xavp); if(keys!=NULL) { do { _cfgt_get_obj_avp_vals(keys->s, avp, jdoc, &jobjt); srjson_AddStrItemToObject(jdoc, jobj, keys->s.s, keys->s.len, jobjt); k = keys; keys = keys->next; pkg_free(k); jobjt = NULL; }while(keys!=NULL); } } if(jobj!=NULL) { srjson_AddItemToArray(jdoc, *jobjr, jobj); jobj = NULL; } }while((xavp = xavp_get_next(xavp))!=0); return 0; }
/** * json serialization of dialog profiles */ int dlg_profiles_to_json(dlg_cell_t *dlg, srjson_doc_t *jdoc) { dlg_profile_link_t *l; srjson_t *aj = NULL; srjson_t *pj = NULL; LM_DBG("serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); if(dlg==NULL || dlg->profile_links==NULL) return -1; LM_DBG("start of serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); for (l = dlg->profile_links ; l ; l=l->next) { if(aj==NULL) { aj = srjson_CreateArray(jdoc); if(aj==NULL) { LM_ERR("cannot create json profiles array object\n"); goto error; } } pj = srjson_CreateObject(jdoc); if(pj==NULL) { LM_ERR("cannot create json dynamic profiles obj\n"); goto error; } srjson_AddStrStrToObject(jdoc, pj, "name", 4, l->profile->name.s, l->profile->name.len); if(l->profile->has_value) { srjson_AddStrStrToObject(jdoc, pj, "value", 5, l->hash_linker.value.s, l->hash_linker.value.len); } if(l->hash_linker.puid[0]!='\0') srjson_AddStringToObject(jdoc, pj, "puid", l->hash_linker.puid); if(l->hash_linker.expires!=0) srjson_AddNumberToObject(jdoc, pj, "expires", l->hash_linker.expires); if(l->hash_linker.flags!=0) srjson_AddNumberToObject(jdoc, pj, "flags", l->hash_linker.flags); srjson_AddItemToArray(jdoc, aj, pj); } if(jdoc->root==NULL) { jdoc->root = srjson_CreateObject(jdoc); if(jdoc->root==NULL) { LM_ERR("cannot create json root\n"); goto error; } } if(aj!=NULL) srjson_AddItemToObject(jdoc, jdoc->root, "profiles", aj); if(jdoc->buf.s != NULL) { jdoc->free_fn(jdoc->buf.s); jdoc->buf.s = NULL; jdoc->buf.len = 0; } jdoc->buf.s = srjson_PrintUnformatted(jdoc, jdoc->root); if(jdoc->buf.s!=NULL) { jdoc->buf.len = strlen(jdoc->buf.s); LM_DBG("serialized profiles for dlg[%u:%u] = [[%.*s]]\n", dlg->h_entry, dlg->h_id, jdoc->buf.len, jdoc->buf.s); return 0; } return -1; error: srjson_Delete(jdoc, aj); return -1; }
/** * json serialization of dialog profiles */ int dlg_profiles_to_json(dlg_cell_t *dlg, srjson_doc_t *jdoc) { dlg_profile_link_t *l; srjson_t *sj = NULL; srjson_t *dj = NULL; LM_DBG("serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); if(dlg==NULL || dlg->profile_links==NULL) return -1; LM_DBG("start of serializing profiles for dlg[%u:%u]\n", dlg->h_entry, dlg->h_id); for (l = dlg->profile_links ; l ; l=l->next) { if(l->profile->has_value) { if(dj==NULL) { dj = srjson_CreateObject(jdoc); if(dj==NULL) { LM_ERR("cannot create json dynamic profiles obj\n"); goto error; } } srjson_AddStrStrToObject(jdoc, dj, l->profile->name.s, l->profile->name.len, l->hash_linker.value.s, l->hash_linker.value.len); } else { if(sj==NULL) { sj = srjson_CreateArray(jdoc); if(sj==NULL) { LM_ERR("cannot create json static profiles obj\n"); goto error; } } srjson_AddItemToArray(jdoc, sj, srjson_CreateStr(jdoc, l->profile->name.s, l->profile->name.len)); } } if(jdoc->root==NULL) { jdoc->root = srjson_CreateObject(jdoc); if(jdoc->root==NULL) { LM_ERR("cannot create json root\n"); goto error; } } if(dj!=NULL) srjson_AddItemToObject(jdoc, jdoc->root, "dprofiles", dj); if(sj!=NULL) srjson_AddItemToObject(jdoc, jdoc->root, "sprofiles", sj); if(jdoc->buf.s != NULL) { jdoc->free_fn(jdoc->buf.s); jdoc->buf.s = NULL; jdoc->buf.len = 0; } jdoc->buf.s = srjson_PrintUnformatted(jdoc, jdoc->root); if(jdoc->buf.s!=NULL) { jdoc->buf.len = strlen(jdoc->buf.s); LM_DBG("serialized profiles for dlg[%u:%u] = [[%.*s]]\n", dlg->h_entry, dlg->h_id, jdoc->buf.len, jdoc->buf.s); return 0; } return -1; error: srjson_Delete(jdoc, dj); srjson_Delete(jdoc, sj); return -1; }
void srjson_AddItemReferenceToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item) { srjson_AddItemToArray(doc, array, create_reference(doc, item)); }